You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DfsInserter.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.dfs;
  44. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  45. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_OFS_DELTA;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_REF_DELTA;
  48. import java.io.BufferedInputStream;
  49. import java.io.EOFException;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.nio.ByteBuffer;
  54. import java.security.MessageDigest;
  55. import java.text.MessageFormat;
  56. import java.util.Collection;
  57. import java.util.Collections;
  58. import java.util.HashSet;
  59. import java.util.List;
  60. import java.util.Set;
  61. import java.util.zip.CRC32;
  62. import java.util.zip.DataFormatException;
  63. import java.util.zip.Deflater;
  64. import java.util.zip.DeflaterOutputStream;
  65. import java.util.zip.Inflater;
  66. import java.util.zip.InflaterInputStream;
  67. import org.eclipse.jgit.errors.CorruptObjectException;
  68. import org.eclipse.jgit.errors.LargeObjectException;
  69. import org.eclipse.jgit.internal.JGitText;
  70. import org.eclipse.jgit.internal.storage.file.PackIndex;
  71. import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
  72. import org.eclipse.jgit.internal.storage.pack.PackExt;
  73. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  74. import org.eclipse.jgit.lib.AnyObjectId;
  75. import org.eclipse.jgit.lib.Constants;
  76. import org.eclipse.jgit.lib.ObjectId;
  77. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  78. import org.eclipse.jgit.lib.ObjectInserter;
  79. import org.eclipse.jgit.lib.ObjectLoader;
  80. import org.eclipse.jgit.lib.ObjectReader;
  81. import org.eclipse.jgit.lib.ObjectStream;
  82. import org.eclipse.jgit.transport.PackedObjectInfo;
  83. import org.eclipse.jgit.util.BlockList;
  84. import org.eclipse.jgit.util.IO;
  85. import org.eclipse.jgit.util.NB;
  86. import org.eclipse.jgit.util.TemporaryBuffer;
  87. import org.eclipse.jgit.util.io.CountingOutputStream;
  88. /** Inserts objects into the DFS. */
  89. public class DfsInserter extends ObjectInserter {
  90. /** Always produce version 2 indexes, to get CRC data. */
  91. private static final int INDEX_VERSION = 2;
  92. private final DfsObjDatabase db;
  93. private int compression = Deflater.BEST_COMPRESSION;
  94. private List<PackedObjectInfo> objectList;
  95. private ObjectIdOwnerMap<PackedObjectInfo> objectMap;
  96. private DfsBlockCache cache;
  97. private DfsPackKey packKey;
  98. private DfsPackDescription packDsc;
  99. private PackStream packOut;
  100. private boolean rollback;
  101. /**
  102. * Initialize a new inserter.
  103. *
  104. * @param db
  105. * database the inserter writes to.
  106. */
  107. protected DfsInserter(DfsObjDatabase db) {
  108. this.db = db;
  109. }
  110. void setCompressionLevel(int compression) {
  111. this.compression = compression;
  112. }
  113. @Override
  114. public DfsPackParser newPackParser(InputStream in) throws IOException {
  115. return new DfsPackParser(db, this, in);
  116. }
  117. @Override
  118. public ObjectReader newReader() {
  119. return new Reader();
  120. }
  121. @Override
  122. public ObjectId insert(int type, byte[] data, int off, int len)
  123. throws IOException {
  124. ObjectId id = idFor(type, data, off, len);
  125. if (objectMap != null && objectMap.contains(id))
  126. return id;
  127. if (db.has(id))
  128. return id;
  129. long offset = beginObject(type, len);
  130. packOut.compress.write(data, off, len);
  131. packOut.compress.finish();
  132. return endObject(id, offset);
  133. }
  134. @Override
  135. public ObjectId insert(int type, long len, InputStream in)
  136. throws IOException {
  137. byte[] buf = insertBuffer(len);
  138. if (len <= buf.length) {
  139. IO.readFully(in, buf, 0, (int) len);
  140. return insert(type, buf, 0, (int) len);
  141. }
  142. long offset = beginObject(type, len);
  143. MessageDigest md = digest();
  144. md.update(Constants.encodedTypeString(type));
  145. md.update((byte) ' ');
  146. md.update(Constants.encodeASCII(len));
  147. md.update((byte) 0);
  148. while (0 < len) {
  149. int n = in.read(buf, 0, (int) Math.min(buf.length, len));
  150. if (n <= 0)
  151. throw new EOFException();
  152. md.update(buf, 0, n);
  153. packOut.compress.write(buf, 0, n);
  154. len -= n;
  155. }
  156. packOut.compress.finish();
  157. return endObject(ObjectId.fromRaw(md.digest()), offset);
  158. }
  159. private byte[] insertBuffer(long len) {
  160. byte[] buf = buffer();
  161. if (len <= buf.length)
  162. return buf;
  163. if (len < db.getReaderOptions().getStreamFileThreshold()) {
  164. try {
  165. return new byte[(int) len];
  166. } catch (OutOfMemoryError noMem) {
  167. return buf;
  168. }
  169. }
  170. return buf;
  171. }
  172. @Override
  173. public void flush() throws IOException {
  174. if (packDsc == null)
  175. return;
  176. if (packOut == null)
  177. throw new IOException();
  178. byte[] packHash = packOut.writePackFooter();
  179. packDsc.addFileExt(PACK);
  180. packDsc.setFileSize(PACK, packOut.getCount());
  181. packOut.close();
  182. packOut = null;
  183. sortObjectsById();
  184. PackIndex index = writePackIndex(packDsc, packHash, objectList);
  185. db.commitPack(Collections.singletonList(packDsc), null);
  186. rollback = false;
  187. DfsPackFile p = cache.getOrCreate(packDsc, packKey);
  188. if (index != null)
  189. p.setPackIndex(index);
  190. db.addPack(p);
  191. clear();
  192. }
  193. @Override
  194. public void close() {
  195. if (packOut != null) {
  196. try {
  197. packOut.close();
  198. } catch (IOException err) {
  199. // Ignore a close failure, the pack should be removed.
  200. } finally {
  201. packOut = null;
  202. }
  203. }
  204. if (rollback && packDsc != null) {
  205. try {
  206. db.rollbackPack(Collections.singletonList(packDsc));
  207. } finally {
  208. packDsc = null;
  209. rollback = false;
  210. }
  211. }
  212. clear();
  213. }
  214. private void clear() {
  215. objectList = null;
  216. objectMap = null;
  217. packKey = null;
  218. packDsc = null;
  219. }
  220. private long beginObject(int type, long len) throws IOException {
  221. if (packOut == null)
  222. beginPack();
  223. long offset = packOut.getCount();
  224. packOut.beginObject(type, len);
  225. return offset;
  226. }
  227. private ObjectId endObject(ObjectId id, long offset) {
  228. PackedObjectInfo obj = new PackedObjectInfo(id);
  229. obj.setOffset(offset);
  230. obj.setCRC((int) packOut.crc32.getValue());
  231. objectList.add(obj);
  232. objectMap.addIfAbsent(obj);
  233. return id;
  234. }
  235. private void beginPack() throws IOException {
  236. objectList = new BlockList<PackedObjectInfo>();
  237. objectMap = new ObjectIdOwnerMap<PackedObjectInfo>();
  238. cache = DfsBlockCache.getInstance();
  239. rollback = true;
  240. packDsc = db.newPack(DfsObjDatabase.PackSource.INSERT);
  241. packOut = new PackStream(db.writeFile(packDsc, PACK));
  242. packKey = new DfsPackKey();
  243. // Write the header as though it were a single object pack.
  244. byte[] buf = packOut.hdrBuf;
  245. System.arraycopy(Constants.PACK_SIGNATURE, 0, buf, 0, 4);
  246. NB.encodeInt32(buf, 4, 2); // Always use pack version 2.
  247. NB.encodeInt32(buf, 8, 1); // Always assume 1 object.
  248. packOut.write(buf, 0, 12);
  249. }
  250. private void sortObjectsById() {
  251. Collections.sort(objectList);
  252. }
  253. PackIndex writePackIndex(DfsPackDescription pack, byte[] packHash,
  254. List<PackedObjectInfo> list) throws IOException {
  255. pack.setIndexVersion(INDEX_VERSION);
  256. pack.setObjectCount(list.size());
  257. // If there are less than 58,000 objects, the entire index fits in under
  258. // 2 MiB. Callers will probably need the index immediately, so buffer
  259. // the index in process and load from the buffer.
  260. TemporaryBuffer.Heap buf = null;
  261. PackIndex packIndex = null;
  262. if (list.size() <= 58000) {
  263. buf = new TemporaryBuffer.Heap(2 << 20);
  264. index(buf, packHash, list);
  265. packIndex = PackIndex.read(buf.openInputStream());
  266. }
  267. DfsOutputStream os = db.writeFile(pack, INDEX);
  268. try {
  269. CountingOutputStream cnt = new CountingOutputStream(os);
  270. if (buf != null)
  271. buf.writeTo(cnt, null);
  272. else
  273. index(cnt, packHash, list);
  274. pack.addFileExt(INDEX);
  275. pack.setFileSize(INDEX, cnt.getCount());
  276. } finally {
  277. os.close();
  278. }
  279. return packIndex;
  280. }
  281. private static void index(OutputStream out, byte[] packHash,
  282. List<PackedObjectInfo> list) throws IOException {
  283. PackIndexWriter.createVersion(out, INDEX_VERSION).write(list, packHash);
  284. }
  285. private class PackStream extends OutputStream {
  286. private final DfsOutputStream out;
  287. private final MessageDigest md;
  288. private final byte[] hdrBuf;
  289. private final Deflater deflater;
  290. private final int blockSize;
  291. private long currPos; // Position of currBuf[0] in the output stream.
  292. private int currPtr; // Number of bytes in currBuf.
  293. private byte[] currBuf;
  294. final CRC32 crc32;
  295. final DeflaterOutputStream compress;
  296. PackStream(DfsOutputStream out) {
  297. this.out = out;
  298. hdrBuf = new byte[32];
  299. md = Constants.newMessageDigest();
  300. crc32 = new CRC32();
  301. deflater = new Deflater(compression);
  302. compress = new DeflaterOutputStream(this, deflater, 8192);
  303. int size = out.blockSize();
  304. if (size <= 0)
  305. size = cache.getBlockSize();
  306. else if (size < cache.getBlockSize())
  307. size = (cache.getBlockSize() / size) * size;
  308. blockSize = size;
  309. currBuf = new byte[blockSize];
  310. }
  311. long getCount() {
  312. return currPos + currPtr;
  313. }
  314. void beginObject(int objectType, long length) throws IOException {
  315. crc32.reset();
  316. deflater.reset();
  317. write(hdrBuf, 0, encodeTypeSize(objectType, length));
  318. }
  319. private int encodeTypeSize(int type, long rawLength) {
  320. long nextLength = rawLength >>> 4;
  321. hdrBuf[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  322. rawLength = nextLength;
  323. int n = 1;
  324. while (rawLength > 0) {
  325. nextLength >>>= 7;
  326. hdrBuf[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  327. rawLength = nextLength;
  328. }
  329. return n;
  330. }
  331. @Override
  332. public void write(final int b) throws IOException {
  333. hdrBuf[0] = (byte) b;
  334. write(hdrBuf, 0, 1);
  335. }
  336. @Override
  337. public void write(byte[] data, int off, int len) throws IOException {
  338. crc32.update(data, off, len);
  339. md.update(data, off, len);
  340. writeNoHash(data, off, len);
  341. }
  342. private void writeNoHash(byte[] data, int off, int len)
  343. throws IOException {
  344. while (0 < len) {
  345. int n = Math.min(len, currBuf.length - currPtr);
  346. if (n == 0) {
  347. flushBlock();
  348. currBuf = new byte[blockSize];
  349. continue;
  350. }
  351. System.arraycopy(data, off, currBuf, currPtr, n);
  352. off += n;
  353. len -= n;
  354. currPtr += n;
  355. }
  356. }
  357. private void flushBlock() throws IOException {
  358. out.write(currBuf, 0, currPtr);
  359. byte[] buf;
  360. if (currPtr == currBuf.length)
  361. buf = currBuf;
  362. else
  363. buf = copyOf(currBuf, 0, currPtr);
  364. cache.put(new DfsBlock(packKey, currPos, buf));
  365. currPos += currPtr;
  366. currPtr = 0;
  367. currBuf = null;
  368. }
  369. private byte[] copyOf(byte[] src, int ptr, int cnt) {
  370. byte[] dst = new byte[cnt];
  371. System.arraycopy(src, ptr, dst, 0, cnt);
  372. return dst;
  373. }
  374. byte[] writePackFooter() throws IOException {
  375. byte[] packHash = md.digest();
  376. writeNoHash(packHash, 0, packHash.length);
  377. if (currPtr != 0)
  378. flushBlock();
  379. return packHash;
  380. }
  381. int read(long pos, byte[] dst, int ptr, int cnt) throws IOException {
  382. int r = 0;
  383. while (pos < currPos && r < cnt) {
  384. DfsBlock b = getOrLoadBlock(pos);
  385. int n = b.copy(pos, dst, ptr + r, cnt - r);
  386. pos += n;
  387. r += n;
  388. }
  389. if (currPos <= pos && r < cnt) {
  390. int s = (int) (pos - currPos);
  391. int n = Math.min(currPtr - s, cnt - r);
  392. System.arraycopy(currBuf, s, dst, ptr + r, n);
  393. r += n;
  394. }
  395. return r;
  396. }
  397. byte[] inflate(DfsReader ctx, long pos, int len) throws IOException,
  398. DataFormatException {
  399. byte[] dstbuf;
  400. try {
  401. dstbuf = new byte[len];
  402. } catch (OutOfMemoryError noMemory) {
  403. return null; // Caller will switch to large object streaming.
  404. }
  405. Inflater inf = ctx.inflater();
  406. pos += setInput(pos, inf);
  407. for (int dstoff = 0;;) {
  408. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  409. dstoff += n;
  410. if (inf.finished())
  411. return dstbuf;
  412. if (inf.needsInput())
  413. pos += setInput(pos, inf);
  414. else if (n == 0)
  415. throw new DataFormatException();
  416. }
  417. }
  418. private int setInput(long pos, Inflater inf) throws IOException {
  419. if (pos < currPos)
  420. return getOrLoadBlock(pos).setInput(pos, inf);
  421. if (pos < currPos + currPtr) {
  422. int s = (int) (pos - currPos);
  423. int n = currPtr - s;
  424. inf.setInput(currBuf, s, n);
  425. return n;
  426. }
  427. throw new EOFException(DfsText.get().unexpectedEofInPack);
  428. }
  429. private DfsBlock getOrLoadBlock(long pos) throws IOException {
  430. long s = toBlockStart(pos);
  431. DfsBlock b = cache.get(packKey, s);
  432. if (b != null)
  433. return b;
  434. byte[] d = new byte[blockSize];
  435. for (int p = 0; p < blockSize;) {
  436. int n = out.read(s + p, ByteBuffer.wrap(d, p, blockSize - p));
  437. if (n <= 0)
  438. throw new EOFException(DfsText.get().unexpectedEofInPack);
  439. p += n;
  440. }
  441. b = new DfsBlock(packKey, s, d);
  442. cache.put(b);
  443. return b;
  444. }
  445. private long toBlockStart(long pos) {
  446. return (pos / blockSize) * blockSize;
  447. }
  448. @Override
  449. public void close() throws IOException {
  450. deflater.end();
  451. out.close();
  452. }
  453. }
  454. private class Reader extends ObjectReader {
  455. private final DfsReader ctx = new DfsReader(db);
  456. @Override
  457. public ObjectReader newReader() {
  458. return db.newReader();
  459. }
  460. @Override
  461. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  462. throws IOException {
  463. Collection<ObjectId> stored = ctx.resolve(id);
  464. if (objectList == null)
  465. return stored;
  466. Set<ObjectId> r = new HashSet<ObjectId>(stored.size() + 2);
  467. r.addAll(stored);
  468. for (PackedObjectInfo obj : objectList) {
  469. if (id.prefixCompare(obj) == 0)
  470. r.add(obj.copy());
  471. }
  472. return r;
  473. }
  474. @Override
  475. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  476. throws IOException {
  477. if (objectMap == null)
  478. return ctx.open(objectId, typeHint);
  479. PackedObjectInfo obj = objectMap.get(objectId);
  480. if (obj == null)
  481. return ctx.open(objectId, typeHint);
  482. byte[] buf = buffer();
  483. int cnt = packOut.read(obj.getOffset(), buf, 0, 20);
  484. if (cnt <= 0)
  485. throw new EOFException(DfsText.get().unexpectedEofInPack);
  486. int c = buf[0] & 0xff;
  487. int type = (c >> 4) & 7;
  488. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA)
  489. throw new IOException(MessageFormat.format(
  490. DfsText.get().cannotReadBackDelta, Integer.toString(type)));
  491. long sz = c & 0x0f;
  492. int ptr = 1;
  493. int shift = 4;
  494. while ((c & 0x80) != 0) {
  495. if (ptr >= cnt)
  496. throw new EOFException(DfsText.get().unexpectedEofInPack);
  497. c = buf[ptr++] & 0xff;
  498. sz += ((long) (c & 0x7f)) << shift;
  499. shift += 7;
  500. }
  501. long zpos = obj.getOffset() + ptr;
  502. if (sz < ctx.getStreamFileThreshold()) {
  503. byte[] data = inflate(obj, zpos, (int) sz);
  504. if (data != null)
  505. return new ObjectLoader.SmallObject(type, data);
  506. }
  507. return new StreamLoader(obj.copy(), type, sz, packKey, zpos);
  508. }
  509. private byte[] inflate(PackedObjectInfo obj, long zpos, int sz)
  510. throws IOException, CorruptObjectException {
  511. try {
  512. return packOut.inflate(ctx, zpos, sz);
  513. } catch (DataFormatException dfe) {
  514. CorruptObjectException coe = new CorruptObjectException(
  515. MessageFormat.format(
  516. JGitText.get().objectAtHasBadZlibStream,
  517. Long.valueOf(obj.getOffset()),
  518. packDsc.getFileName(PackExt.PACK)));
  519. coe.initCause(dfe);
  520. throw coe;
  521. }
  522. }
  523. @Override
  524. public Set<ObjectId> getShallowCommits() throws IOException {
  525. return ctx.getShallowCommits();
  526. }
  527. @Override
  528. public void close() {
  529. ctx.close();
  530. }
  531. }
  532. private class StreamLoader extends ObjectLoader {
  533. private final ObjectId id;
  534. private final int type;
  535. private final long size;
  536. private final DfsPackKey srcPack;
  537. private final long pos;
  538. StreamLoader(ObjectId id, int type, long sz,
  539. DfsPackKey key, long pos) {
  540. this.id = id;
  541. this.type = type;
  542. this.size = sz;
  543. this.srcPack = key;
  544. this.pos = pos;
  545. }
  546. @Override
  547. public ObjectStream openStream() throws IOException {
  548. final DfsReader ctx = new DfsReader(db);
  549. if (srcPack != packKey) {
  550. try {
  551. // Post DfsInserter.flush() use the normal code path.
  552. // The newly created pack is registered in the cache.
  553. return ctx.open(id, type).openStream();
  554. } finally {
  555. ctx.close();
  556. }
  557. }
  558. int bufsz = 8192;
  559. final Inflater inf = ctx.inflater();
  560. return new ObjectStream.Filter(type,
  561. size, new BufferedInputStream(new InflaterInputStream(
  562. new ReadBackStream(pos), inf, bufsz), bufsz)) {
  563. @Override
  564. public void close() throws IOException {
  565. ctx.close();
  566. super.close();
  567. }
  568. };
  569. }
  570. @Override
  571. public int getType() {
  572. return type;
  573. }
  574. @Override
  575. public long getSize() {
  576. return size;
  577. }
  578. @Override
  579. public boolean isLarge() {
  580. return true;
  581. }
  582. @Override
  583. public byte[] getCachedBytes() throws LargeObjectException {
  584. throw new LargeObjectException.ExceedsLimit(
  585. db.getReaderOptions().getStreamFileThreshold(), size);
  586. }
  587. }
  588. private final class ReadBackStream extends InputStream {
  589. private long pos;
  590. ReadBackStream(long offset) {
  591. pos = offset;
  592. }
  593. @Override
  594. public int read() throws IOException {
  595. byte[] b = new byte[1];
  596. int n = read(b);
  597. return n == 1 ? b[0] & 0xff : -1;
  598. }
  599. @Override
  600. public int read(byte[] buf, int ptr, int len) throws IOException {
  601. int n = packOut.read(pos, buf, ptr, len);
  602. if (n > 0) {
  603. pos += n;
  604. }
  605. return n;
  606. }
  607. }
  608. }