annotate src/org/tmatesoft/hg/internal/InflaterDataAccess.java @ 578:f97e81d13190

Throw exception if repository encountered is too old for Hg4J
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 17 Apr 2013 16:02:52 +0200
parents 3c4db86e8c1f
children ed243b668502
rev   line source
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
2 * Copyright (c) 2011-2013 TMate Software Ltd
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
3 *
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
4 * This program is free software; you can redistribute it and/or modify
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
5 * it under the terms of the GNU General Public License as published by
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
6 * the Free Software Foundation; version 2 of the License.
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
7 *
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
8 * This program is distributed in the hope that it will be useful,
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
11 * GNU General Public License for more details.
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
12 *
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
13 * For information on how to redistribute this software under
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
14 * the terms of a license other than GNU General Public License
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
15 * contact TMate Software at support@hg4j.com
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
17 package org.tmatesoft.hg.internal;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.IOException;
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
20 import java.nio.ByteBuffer;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.zip.DataFormatException;
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.zip.Inflater;
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.zip.ZipException;
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 /**
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 * DataAccess counterpart for InflaterInputStream.
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 * XXX is it really needed to be subclass of FilterDataAccess?
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
28 *
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
29 * @author Artem Tikhomirov
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
30 * @author TMate Software Ltd.
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 */
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 public class InflaterDataAccess extends FilterDataAccess {
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 private final Inflater inflater;
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
35 private final byte[] inBuffer;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
36 private final ByteBuffer outBuffer;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
37 private int inflaterPos = 0;
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
38 private int decompressedLength;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
420
6c22bdc0bdfd Respect long offsets in revlogs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 399
diff changeset
40 public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength) {
263
31f67be94e71 RevlogStream - reduce number of object instances, reuse when possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 158
diff changeset
41 this(dataAccess, offset, compressedLength, -1, new Inflater(), new byte[512]);
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
420
6c22bdc0bdfd Respect long offsets in revlogs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 399
diff changeset
44 public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength, int actualLength) {
263
31f67be94e71 RevlogStream - reduce number of object instances, reuse when possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 158
diff changeset
45 this(dataAccess, offset, compressedLength, actualLength, new Inflater(), new byte[512]);
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
46 }
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
47
420
6c22bdc0bdfd Respect long offsets in revlogs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 399
diff changeset
48 public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength, int actualLength, Inflater inflater, byte[] buf) {
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
49 super(dataAccess, offset, compressedLength);
263
31f67be94e71 RevlogStream - reduce number of object instances, reuse when possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 158
diff changeset
50 if (inflater == null || buf == null) {
31f67be94e71 RevlogStream - reduce number of object instances, reuse when possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 158
diff changeset
51 throw new IllegalArgumentException();
31f67be94e71 RevlogStream - reduce number of object instances, reuse when possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 158
diff changeset
52 }
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 this.inflater = inflater;
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
54 this.decompressedLength = actualLength;
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
55 inBuffer = buf;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
56 outBuffer = ByteBuffer.allocate(inBuffer.length * 2);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
57 outBuffer.limit(0); // there's nothing to read in the buffer
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 @Override
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
61 public InflaterDataAccess reset() throws IOException {
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 super.reset();
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 inflater.reset();
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
64 inflaterPos = 0;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
65 outBuffer.clear().limit(0); // or flip(), to indicate nothing to read
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
66 return this;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 @Override
421
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
70 protected int available() throws IOException {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
71 return length() - decompressedPosition();
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 @Override
421
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
75 public boolean isEmpty() throws IOException {
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
76 // can't use super.available() <= 0 because even when 0 < super.count < 6(?)
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
77 // decompressedPos might be already == length()
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
78 return available() <= 0;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 @Override
421
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
82 public int length() throws IOException {
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 if (decompressedLength != -1) {
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 return decompressedLength;
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
86 decompressedLength = 0; // guard to avoid endless loop in case length() would get invoked from below.
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
87 final int oldPos = decompressedPosition();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
88 final int inflatedUpTo = inflaterPos;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
89 int inflatedMore = 0, c;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
90 do {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
91 outBuffer.limit(outBuffer.position()); // pretend the buffer is consumed
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
92 c = fillOutBuffer();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
93 inflatedMore += c;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
94 } while (c == outBuffer.capacity()); // once we unpacked less than capacity, input is over
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
95 decompressedLength = inflatedUpTo + inflatedMore;
421
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
96 reset();
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
97 seek(oldPos);
fdd7d756dea0 Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 420
diff changeset
98 return decompressedLength;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 @Override
158
b413b16d10a5 Integer offsets and file length explictly, rather than casts throughout code. Inflater may benefit from total length hint, but shall calculate it by its own if needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 157
diff changeset
102 public void seek(int localOffset) throws IOException {
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 if (localOffset < 0 /* || localOffset >= length() */) {
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 throw new IllegalArgumentException();
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
106 int currentPos = decompressedPosition();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
107 if (localOffset >= currentPos) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
108 skip(localOffset - currentPos);
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 } else {
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 reset();
420
6c22bdc0bdfd Respect long offsets in revlogs
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 399
diff changeset
111 skip(localOffset);
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 @Override
391
856517285256 Provide more details about exception
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 263
diff changeset
116 public void skip(final int bytesToSkip) throws IOException {
856517285256 Provide more details about exception
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 263
diff changeset
117 int bytes = bytesToSkip;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 if (bytes < 0) {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
119 bytes += decompressedPosition();
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 if (bytes < 0) {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
121 throw new IOException(String.format("Underflow. Rewind past start of the slice. To skip:%d, decPos:%d, decLen:%d. Left:%d", bytesToSkip, inflaterPos, decompressedLength, bytes));
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 reset();
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 // fall-through
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 while (!isEmpty() && bytes > 0) {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
127 int fromBuffer = outBuffer.remaining();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
128 if (fromBuffer > 0) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
129 if (fromBuffer >= bytes) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
130 outBuffer.position(outBuffer.position() + bytes);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
131 bytes = 0;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
132 break;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
133 } else {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
134 bytes -= fromBuffer;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
135 outBuffer.limit(outBuffer.position()); // mark consumed
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
136 // fall through to fill the buffer
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
137 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
138 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
139 fillOutBuffer();
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 if (bytes != 0) {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
142 throw new IOException(String.format("Underflow. Rewind past end of the slice. To skip:%d, decPos:%d, decLen:%d. Left:%d", bytesToSkip, inflaterPos, decompressedLength, bytes));
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 @Override
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 public byte readByte() throws IOException {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
148 if (!outBuffer.hasRemaining()) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
149 fillOutBuffer();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
150 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
151 return outBuffer.get();
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 @Override
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 public void readBytes(byte[] b, int off, int len) throws IOException {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
156 do {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
157 int fromBuffer = outBuffer.remaining();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
158 if (fromBuffer > 0) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
159 if (fromBuffer >= len) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
160 outBuffer.get(b, off, len);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
161 return;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
162 } else {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
163 outBuffer.get(b, off, fromBuffer);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
164 off += fromBuffer;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
165 len -= fromBuffer;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
166 // fall-through
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
167 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
168 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
169 fillOutBuffer();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
170 } while (len > 0);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
171 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
172
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
173 @Override
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
174 public void readBytes(ByteBuffer buf) throws IOException {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
175 int len = Math.min(available(), buf.remaining());
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
176 while (len > 0) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
177 if (outBuffer.remaining() >= len) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
178 ByteBuffer slice = outBuffer.slice();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
179 slice.limit(len);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
180 buf.put(slice);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
181 outBuffer.position(outBuffer.position() + len);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
182 return;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
183 } else {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
184 len -= outBuffer.remaining();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
185 buf.put(outBuffer);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
186 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
187 fillOutBuffer();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
188 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
189 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
190
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
191 private int decompressedPosition() {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
192 assert outBuffer.remaining() <= inflaterPos;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
193 return inflaterPos - outBuffer.remaining();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
194 }
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
195
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
196 // after #fillOutBuffer(), outBuffer is ready for read
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
197 private int fillOutBuffer() throws IOException {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
198 assert !outBuffer.hasRemaining();
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
199 try {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
200 int inflatedBytes = 0;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
201 outBuffer.clear();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
202 int len = outBuffer.capacity();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
203 int off = 0;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
204 do {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
205 int n;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
206 while ((n = inflater.inflate(outBuffer.array(), off, len)) == 0) {
399
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
207 // XXX few last bytes (checksum?) may be ignored by inflater, thus inflate may return 0 in
157
d5268ca7715b Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 51
diff changeset
208 // perfectly legal conditions (when all data already expanded, but there are still some bytes
399
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
209 // in the input stream)
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
210 int toRead = -1;
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
211 if (inflater.needsInput() && (toRead = super.available()) > 0) {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
212 // fill
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
213 if (toRead > inBuffer.length) {
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
214 toRead = inBuffer.length;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 }
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
216 super.readBytes(inBuffer, 0, toRead);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
217 inflater.setInput(inBuffer, 0, toRead);
399
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
218 } else {
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
219 // inflated nothing and doesn't want any more data (or no date available) - assume we're done
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
220 assert inflater.finished();
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
221 assert toRead <= 0;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
222 break;
399
fdc1db8f7f61 Issue 25: Underflow in InflaterDataAccess; test and fix for hang up when reading past end of compressed data (or zero-length data)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 391
diff changeset
223 // prevent hang up in this cycle if no more data is available, see Issue 25
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
224 // throw new EOFException(String.format("No more compressed data is available to satisfy request for %d bytes. [finished:%b, needDict:%b, needInp:%b, available:%d", len, inflater.finished(), inflater.needsDictionary(), inflater.needsInput(), toRead));
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
226 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 off += n;
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
228 len -= n;
576
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
229 inflatedBytes += n;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
230 } while (len > 0 && !inflater.finished()); // either the buffer is filled or nothing more to unpack
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
231 inflaterPos += inflatedBytes;
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
232 outBuffer.limit(inflatedBytes);
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
233 assert outBuffer.position() == 0; // didn't change since #clear() above
3c4db86e8c1f Issue 43: poor performance with InflaterDataAccess. Phase 2: inflate into buffer, effective skip and readByte/readBytes()
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 575
diff changeset
234 return inflatedBytes;
51
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
235 } catch (DataFormatException e) {
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
236 String s = e.getMessage();
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
237 throw new ZipException(s != null ? s : "Invalid ZLIB data format");
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
238 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
239 }
9429c7bd1920 Try DataAccess to reach revision data instead of plain byte arrays
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
240 }