58 /** 59 * A pointer to the next available byte in the buffer. This is 60 * used to read data from the buffer and must be updated to 61 * move through the buffer. 62 */ 63 int bufPtr; 64 65 /** 66 * The ImageInputStream buffered. 67 */ 68 ImageInputStream iis; 69 70 JPEGBuffer (ImageInputStream iis) { 71 buf = new byte[BUFFER_SIZE]; 72 bufAvail = 0; 73 bufPtr = 0; 74 this.iis = iis; 75 } 76 77 /** 78 * Ensures that there are at least <code>count</code> bytes available 79 * in the buffer, loading more data and moving any remaining 80 * bytes to the front. A count of 0 means to just fill the buffer. 81 * If the count is larger than the buffer size, just fills the buffer. 82 * If the end of the stream is encountered before a non-0 count can 83 * be satisfied, an <code>IIOException</code> is thrown with the 84 * message "Image Format Error". 85 */ 86 void loadBuf(int count) throws IOException { 87 if (debug) { 88 System.out.print("loadbuf called with "); 89 System.out.print("count " + count + ", "); 90 System.out.println("bufAvail " + bufAvail + ", "); 91 } 92 if (count != 0) { 93 if (bufAvail >= count) { // have enough 94 return; 95 } 96 } else { 97 if (bufAvail == BUFFER_SIZE) { // already full 98 return; 99 } 100 } 101 // First copy any remaining bytes down to the beginning 102 if ((bufAvail > 0) && (bufAvail < BUFFER_SIZE)) { 103 System.arraycopy(buf, bufPtr, buf, 0, bufAvail); 105 // Now fill the rest of the buffer 106 int ret = iis.read(buf, bufAvail, buf.length - bufAvail); 107 if (debug) { 108 System.out.println("iis.read returned " + ret); 109 } 110 if (ret != -1) { 111 bufAvail += ret; 112 } 113 bufPtr = 0; 114 int minimum = Math.min(BUFFER_SIZE, count); 115 if (bufAvail < minimum) { 116 throw new IIOException ("Image Format Error"); 117 } 118 } 119 120 /** 121 * Fills the data array from the stream, starting with 122 * the buffer and then reading directly from the stream 123 * if necessary. The buffer is left in an appropriate 124 * state. If the end of the stream is encountered, an 125 * <code>IIOException</code> is thrown with the 126 * message "Image Format Error". 127 */ 128 void readData(byte [] data) throws IOException { 129 int count = data.length; 130 // First see what's left in the buffer. 131 if (bufAvail >= count) { // It's enough 132 System.arraycopy(buf, bufPtr, data, 0, count); 133 bufAvail -= count; 134 bufPtr += count; 135 return; 136 } 137 int offset = 0; 138 if (bufAvail > 0) { // Some there, but not enough 139 System.arraycopy(buf, bufPtr, data, 0, bufAvail); 140 offset = bufAvail; 141 count -= bufAvail; 142 bufAvail = 0; 143 bufPtr = 0; 144 } 145 // Now read the rest directly from the stream 146 if (iis.read(data, offset, count) != count) { 147 throw new IIOException ("Image format Error"); 148 } 149 } 150 151 /** 152 * Skips <code>count</code> bytes, leaving the buffer 153 * in an appropriate state. If the end of the stream is 154 * encountered, an <code>IIOException</code> is thrown with the 155 * message "Image Format Error". 156 */ 157 void skipData(int count) throws IOException { 158 // First see what's left in the buffer. 159 if (bufAvail >= count) { // It's enough 160 bufAvail -= count; 161 bufPtr += count; 162 return; 163 } 164 if (bufAvail > 0) { // Some there, but not enough 165 count -= bufAvail; 166 bufAvail = 0; 167 bufPtr = 0; 168 } 169 // Now read the rest directly from the stream 170 if (iis.skipBytes(count) != count) { 171 throw new IIOException ("Image format Error"); 172 } 173 } 174 178 */ 179 void pushBack() throws IOException { 180 iis.seek(iis.getStreamPosition()-bufAvail); 181 bufAvail = 0; 182 bufPtr = 0; 183 } 184 185 /** 186 * Return the stream position corresponding to the next 187 * available byte in the buffer. 188 */ 189 long getStreamPosition() throws IOException { 190 return (iis.getStreamPosition()-bufAvail); 191 } 192 193 /** 194 * Scan the buffer until the next 0xff byte, reloading 195 * the buffer as necessary. The buffer position is left 196 * pointing to the first non-0xff byte after a run of 197 * 0xff bytes. If the end of the stream is encountered, 198 * an EOI marker is inserted into the buffer and <code>true</code> 199 * is returned. Otherwise returns <code>false</code>. 200 */ 201 boolean scanForFF(JPEGImageReader reader) throws IOException { 202 boolean retval = false; 203 boolean foundFF = false; 204 while (foundFF == false) { 205 while (bufAvail > 0) { 206 if ((buf[bufPtr++] & 0xff) == 0xff) { 207 bufAvail--; 208 foundFF = true; 209 break; // out of inner while 210 } 211 bufAvail--; 212 } 213 // Reload the buffer and keep going 214 loadBuf(0); 215 // Skip any remaining pad bytes 216 if (foundFF == true) { 217 while ((bufAvail > 0) && (buf[bufPtr] & 0xff) == 0xff) { 218 bufPtr++; // Only if it still is 0xff 219 bufAvail--; | 58 /** 59 * A pointer to the next available byte in the buffer. This is 60 * used to read data from the buffer and must be updated to 61 * move through the buffer. 62 */ 63 int bufPtr; 64 65 /** 66 * The ImageInputStream buffered. 67 */ 68 ImageInputStream iis; 69 70 JPEGBuffer (ImageInputStream iis) { 71 buf = new byte[BUFFER_SIZE]; 72 bufAvail = 0; 73 bufPtr = 0; 74 this.iis = iis; 75 } 76 77 /** 78 * Ensures that there are at least {@code count} bytes available 79 * in the buffer, loading more data and moving any remaining 80 * bytes to the front. A count of 0 means to just fill the buffer. 81 * If the count is larger than the buffer size, just fills the buffer. 82 * If the end of the stream is encountered before a non-0 count can 83 * be satisfied, an {@code IIOException} is thrown with the 84 * message "Image Format Error". 85 */ 86 void loadBuf(int count) throws IOException { 87 if (debug) { 88 System.out.print("loadbuf called with "); 89 System.out.print("count " + count + ", "); 90 System.out.println("bufAvail " + bufAvail + ", "); 91 } 92 if (count != 0) { 93 if (bufAvail >= count) { // have enough 94 return; 95 } 96 } else { 97 if (bufAvail == BUFFER_SIZE) { // already full 98 return; 99 } 100 } 101 // First copy any remaining bytes down to the beginning 102 if ((bufAvail > 0) && (bufAvail < BUFFER_SIZE)) { 103 System.arraycopy(buf, bufPtr, buf, 0, bufAvail); 105 // Now fill the rest of the buffer 106 int ret = iis.read(buf, bufAvail, buf.length - bufAvail); 107 if (debug) { 108 System.out.println("iis.read returned " + ret); 109 } 110 if (ret != -1) { 111 bufAvail += ret; 112 } 113 bufPtr = 0; 114 int minimum = Math.min(BUFFER_SIZE, count); 115 if (bufAvail < minimum) { 116 throw new IIOException ("Image Format Error"); 117 } 118 } 119 120 /** 121 * Fills the data array from the stream, starting with 122 * the buffer and then reading directly from the stream 123 * if necessary. The buffer is left in an appropriate 124 * state. If the end of the stream is encountered, an 125 * {@code IIOException} is thrown with the 126 * message "Image Format Error". 127 */ 128 void readData(byte [] data) throws IOException { 129 int count = data.length; 130 // First see what's left in the buffer. 131 if (bufAvail >= count) { // It's enough 132 System.arraycopy(buf, bufPtr, data, 0, count); 133 bufAvail -= count; 134 bufPtr += count; 135 return; 136 } 137 int offset = 0; 138 if (bufAvail > 0) { // Some there, but not enough 139 System.arraycopy(buf, bufPtr, data, 0, bufAvail); 140 offset = bufAvail; 141 count -= bufAvail; 142 bufAvail = 0; 143 bufPtr = 0; 144 } 145 // Now read the rest directly from the stream 146 if (iis.read(data, offset, count) != count) { 147 throw new IIOException ("Image format Error"); 148 } 149 } 150 151 /** 152 * Skips {@code count} bytes, leaving the buffer 153 * in an appropriate state. If the end of the stream is 154 * encountered, an {@code IIOException} is thrown with the 155 * message "Image Format Error". 156 */ 157 void skipData(int count) throws IOException { 158 // First see what's left in the buffer. 159 if (bufAvail >= count) { // It's enough 160 bufAvail -= count; 161 bufPtr += count; 162 return; 163 } 164 if (bufAvail > 0) { // Some there, but not enough 165 count -= bufAvail; 166 bufAvail = 0; 167 bufPtr = 0; 168 } 169 // Now read the rest directly from the stream 170 if (iis.skipBytes(count) != count) { 171 throw new IIOException ("Image format Error"); 172 } 173 } 174 178 */ 179 void pushBack() throws IOException { 180 iis.seek(iis.getStreamPosition()-bufAvail); 181 bufAvail = 0; 182 bufPtr = 0; 183 } 184 185 /** 186 * Return the stream position corresponding to the next 187 * available byte in the buffer. 188 */ 189 long getStreamPosition() throws IOException { 190 return (iis.getStreamPosition()-bufAvail); 191 } 192 193 /** 194 * Scan the buffer until the next 0xff byte, reloading 195 * the buffer as necessary. The buffer position is left 196 * pointing to the first non-0xff byte after a run of 197 * 0xff bytes. If the end of the stream is encountered, 198 * an EOI marker is inserted into the buffer and {@code true} 199 * is returned. Otherwise returns {@code false}. 200 */ 201 boolean scanForFF(JPEGImageReader reader) throws IOException { 202 boolean retval = false; 203 boolean foundFF = false; 204 while (foundFF == false) { 205 while (bufAvail > 0) { 206 if ((buf[bufPtr++] & 0xff) == 0xff) { 207 bufAvail--; 208 foundFF = true; 209 break; // out of inner while 210 } 211 bufAvail--; 212 } 213 // Reload the buffer and keep going 214 loadBuf(0); 215 // Skip any remaining pad bytes 216 if (foundFF == true) { 217 while ((bufAvail > 0) && (buf[bufPtr] & 0xff) == 0xff) { 218 bufPtr++; // Only if it still is 0xff 219 bufAvail--; |