1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.stream.ImageInputStream ; 11 import javax.imageio.IIOException ; 12 13 import java.io.IOException ; 14 15 19 class JPEGBuffer { 20 21 private boolean debug = false; 22 23 27 final int BUFFER_SIZE = 4096; 28 29 32 byte [] buf; 33 34 38 int bufAvail; 39 40 45 int bufPtr; 46 47 50 ImageInputStream iis; 51 52 JPEGBuffer (ImageInputStream iis) { 53 buf = new byte[BUFFER_SIZE]; 54 bufAvail = 0; 55 bufPtr = 0; 56 this.iis = iis; 57 } 58 59 68 void loadBuf(int count) throws IOException { 69 if (debug) { 70 System.out.print("loadbuf called with "); 71 System.out.print("count " + count + ", "); 72 System.out.println("bufAvail " + bufAvail + ", "); 73 } 74 if (count != 0) { 75 if (bufAvail >= count) { return; 77 } 78 } else { 79 if (bufAvail == BUFFER_SIZE) { return; 81 } 82 } 83 if ((bufAvail > 0) && (bufAvail < BUFFER_SIZE)) { 85 System.arraycopy(buf, bufPtr, buf, 0, bufAvail); 86 } 87 int ret = iis.read(buf, bufAvail, buf.length - bufAvail); 89 if (debug) { 90 System.out.println("iis.read returned " + ret); 91 } 92 if (ret != -1) { 93 bufAvail += ret; 94 } 95 bufPtr = 0; 96 int minimum = Math.min(BUFFER_SIZE, count); 97 if (bufAvail < minimum) { 98 throw new IIOException ("Image Format Error"); 99 } 100 } 101 102 110 void readData(byte [] data) throws IOException { 111 int count = data.length; 112 if (bufAvail >= count) { System.arraycopy(buf, bufPtr, data, 0, count); 115 bufAvail -= count; 116 bufPtr += count; 117 return; 118 } 119 int offset = 0; 120 if (bufAvail > 0) { System.arraycopy(buf, bufPtr, data, 0, bufAvail); 122 offset = bufAvail; 123 count -= bufAvail; 124 bufAvail = 0; 125 bufPtr = 0; 126 } 127 if (iis.read(data, offset, count) != count) { 129 throw new IIOException ("Image format Error"); 130 } 131 } 132 133 139 void skipData(int count) throws IOException { 140 if (bufAvail >= count) { bufAvail -= count; 143 bufPtr += count; 144 return; 145 } 146 if (bufAvail > 0) { count -= bufAvail; 148 bufAvail = 0; 149 bufPtr = 0; 150 } 151 if (iis.skipBytes(count) != count) { 153 throw new IIOException ("Image format Error"); 154 } 155 } 156 157 161 void pushBack() throws IOException { 162 iis.seek(iis.getStreamPosition()-bufAvail); 163 bufAvail = 0; 164 bufPtr = 0; 165 } 166 167 171 long getStreamPosition() throws IOException { 172 return (iis.getStreamPosition()-bufAvail); 173 } 174 175 183 boolean scanForFF(JPEGImageReader reader) throws IOException { 184 boolean retval = false; 185 boolean foundFF = false; 186 while (foundFF == false) { 187 while (bufAvail > 0) { 188 if ((buf[bufPtr++] & 0xff) == 0xff) { 189 bufAvail--; 190 foundFF = true; 191 break; } 193 bufAvail--; 194 } 195 loadBuf(0); 197 if (foundFF == true) { 199 while ((bufAvail > 0) && (buf[bufPtr] & 0xff) == 0xff) { 200 bufPtr++; bufAvail--; 202 } 203 } 204 if (bufAvail == 0) { retval = true; 208 buf[0] = (byte)JPEG.EOI; 209 bufAvail = 1; 210 bufPtr = 0; 211 foundFF = true; 212 } 213 } 214 return retval; 215 } 216 217 222 void print(int count) { 223 System.out.print("buffer has "); 224 System.out.print(bufAvail); 225 System.out.println(" bytes available"); 226 if (bufAvail < count) { 227 count = bufAvail; 228 } 229 for (int ptr = bufPtr; count > 0; count--) { 230 int val = (int)buf[ptr++] & 0xff; 231 System.out.print(" " + Integer.toHexString(val)); 232 } 233 System.out.println(); 234 } 235 236 } 237 238 | Popular Tags |