View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import java.io.BufferedInputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.util.zip.InflaterInputStream;
17  
18  import org.eclipse.jgit.errors.LargeObjectException;
19  import org.eclipse.jgit.errors.MissingObjectException;
20  import org.eclipse.jgit.lib.ObjectId;
21  import org.eclipse.jgit.lib.ObjectLoader;
22  import org.eclipse.jgit.lib.ObjectStream;
23  
24  class LargePackedWholeObject extends ObjectLoader {
25  	private final int type;
26  
27  	private final long size;
28  
29  	private final long objectOffset;
30  
31  	private final int headerLength;
32  
33  	private final PackFile pack;
34  
35  	private final FileObjectDatabase db;
36  
37  	LargePackedWholeObject(int type, long size, long objectOffset,
38  			int headerLength, PackFile pack, FileObjectDatabase db) {
39  		this.type = type;
40  		this.size = size;
41  		this.objectOffset = objectOffset;
42  		this.headerLength = headerLength;
43  		this.pack = pack;
44  		this.db = db;
45  	}
46  
47  	/** {@inheritDoc} */
48  	@Override
49  	public int getType() {
50  		return type;
51  	}
52  
53  	/** {@inheritDoc} */
54  	@Override
55  	public long getSize() {
56  		return size;
57  	}
58  
59  	/** {@inheritDoc} */
60  	@Override
61  	public boolean isLarge() {
62  		return true;
63  	}
64  
65  	/** {@inheritDoc} */
66  	@Override
67  	public byte[] getCachedBytes() throws LargeObjectException {
68  		try {
69  			throw new LargeObjectException(getObjectId());
70  		} catch (IOException cannotObtainId) {
71  			throw new LargeObjectException(cannotObtainId);
72  		}
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	public ObjectStream openStream() throws MissingObjectException, IOException {
78  		WindowCursor wc = new WindowCursor(db);
79  		InputStream in;
80  		try {
81  			in = new PackInputStream(pack, objectOffset + headerLength, wc);
82  		} catch (IOException packGone) {
83  			// If the pack file cannot be pinned into the cursor, it
84  			// probably was repacked recently. Go find the object
85  			// again and open the stream from that location instead.
86  			//
87  			return wc.open(getObjectId(), type).openStream();
88  		}
89  
90  		in = new BufferedInputStream( //
91  				new InflaterInputStream( //
92  						in, //
93  						wc.inflater(), //
94  						8192), //
95  				8192);
96  		return new ObjectStream.Filter(type, size, in);
97  	}
98  
99  	private ObjectId getObjectId() throws IOException {
100 		return pack.findObjectForOffset(objectOffset);
101 	}
102 }