comparison src/org/tmatesoft/hg/internal/InflaterDataAccess.java @ 421:fdd7d756dea0 v0.8.5

Allow IOException from DataAccess methods for subclasses with non-trivial implementations, to avoid exception dumps when inapropriate
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 22 Mar 2012 23:09:11 +0100
parents 6c22bdc0bdfd
children 9c9c442b5f2e
comparison
equal deleted inserted replaced
420:6c22bdc0bdfd 421:fdd7d756dea0
65 decompressedPos = 0; 65 decompressedPos = 0;
66 return this; 66 return this;
67 } 67 }
68 68
69 @Override 69 @Override
70 protected int available() { 70 protected int available() throws IOException {
71 return length() - decompressedPos; 71 return length() - decompressedPos;
72 } 72 }
73 73
74 @Override 74 @Override
75 public boolean isEmpty() { 75 public boolean isEmpty() throws IOException {
76 // can't use super.available() <= 0 because even when 0 < super.count < 6(?) 76 // can't use super.available() <= 0 because even when 0 < super.count < 6(?)
77 // decompressedPos might be already == length() 77 // decompressedPos might be already == length()
78 return available() <= 0; 78 return available() <= 0;
79 } 79 }
80 80
81 @Override 81 @Override
82 public int length() { 82 public int length() throws IOException {
83 if (decompressedLength != -1) { 83 if (decompressedLength != -1) {
84 return decompressedLength; 84 return decompressedLength;
85 } 85 }
86 decompressedLength = 0; // guard to avoid endless loop in case length() would get invoked from below. 86 decompressedLength = 0; // guard to avoid endless loop in case length() would get invoked from below.
87 int c = 0; 87 int c = 0;
88 try { 88 int oldPos = decompressedPos;
89 int oldPos = decompressedPos; 89 byte[] dummy = new byte[buffer.length];
90 byte[] dummy = new byte[buffer.length]; 90 int toRead;
91 int toRead; 91 while ((toRead = super.available()) > 0) {
92 while ((toRead = super.available()) > 0) { 92 if (toRead > buffer.length) {
93 if (toRead > buffer.length) { 93 toRead = buffer.length;
94 toRead = buffer.length; 94 }
95 super.readBytes(buffer, 0, toRead);
96 inflater.setInput(buffer, 0, toRead);
97 try {
98 while (!inflater.needsInput()) {
99 c += inflater.inflate(dummy, 0, dummy.length);
95 } 100 }
96 super.readBytes(buffer, 0, toRead); 101 } catch (DataFormatException ex) {
97 inflater.setInput(buffer, 0, toRead); 102 throw new HgBadStateException(ex);
98 try {
99 while (!inflater.needsInput()) {
100 c += inflater.inflate(dummy, 0, dummy.length);
101 }
102 } catch (DataFormatException ex) {
103 throw new HgBadStateException(ex);
104 }
105 } 103 }
106 decompressedLength = c + oldPos;
107 reset();
108 seek(oldPos);
109 return decompressedLength;
110 } catch (IOException ex) {
111 decompressedLength = -1; // better luck next time?
112 throw new HgBadStateException(ex); // XXX perhaps, checked exception
113 } 104 }
105 decompressedLength = c + oldPos;
106 reset();
107 seek(oldPos);
108 return decompressedLength;
114 } 109 }
115 110
116 @Override 111 @Override
117 public void seek(int localOffset) throws IOException { 112 public void seek(int localOffset) throws IOException {
118 if (localOffset < 0 /* || localOffset >= length() */) { 113 if (localOffset < 0 /* || localOffset >= length() */) {