1 31 package org.pdfbox.io; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 37 59 public class ByteArrayPushBackInputStream extends PushBackInputStream 60 { 61 private byte[] data; 62 private int datapos; 63 private int datalen; 64 private int save; 65 66 private static final InputStream DUMMY = new ByteArrayInputStream ("".getBytes()); 68 69 76 public ByteArrayPushBackInputStream(byte[] input) throws IOException 77 { 78 super(DUMMY, 1); 79 data = input; 80 datapos = 0; 81 save = datapos; 82 datalen = input != null ? input.length : 0; 83 } 84 85 90 public int peek() 91 { 92 try 93 { 94 return (data[datapos] + 0x100) & 0xff; 96 } 97 catch (ArrayIndexOutOfBoundsException ex) 98 { 99 return -1; 103 } 104 } 105 106 111 public boolean isEOF() 112 { 113 return datapos >= datalen; 114 } 115 116 121 public void mark(int readlimit) 122 { 123 if (false) 124 { 125 ++readlimit; } 127 save = datapos; 128 } 129 130 135 public boolean markSupported() 136 { 137 return true; 138 } 139 140 144 public void reset() 145 { 146 datapos = save; 147 } 148 149 153 public int available() 154 { 155 int av = datalen - datapos; 156 return av > 0 ? av : 0; 157 } 158 159 162 public int size() 163 { 164 return datalen; 165 } 166 167 174 public void unread(int by) throws IOException 175 { 176 if (datapos == 0) 177 { 178 throw new IOException ("ByteArrayParserInputStream.unread(int): " + 179 "cannot unread 1 byte at buffer position " + datapos); 180 } 181 --datapos; 182 data[datapos] = (byte)by; 183 } 184 185 197 public void unread(byte[] buffer, int off, int len) throws IOException 198 { 199 if (len <= 0 || off >= buffer.length) 200 { 201 return; 202 } 203 if (off < 0) 204 { 205 off = 0; 206 } 207 if (len > buffer.length) 208 { 209 len = buffer.length; 210 } 211 localUnread(buffer, off, len); 212 } 213 214 224 public void unread(byte[] buffer) throws IOException 225 { 226 localUnread(buffer, 0, buffer.length); 227 } 228 229 242 private void localUnread(byte[] buffer, int off, int len) throws IOException 243 { 244 if (datapos < len) 245 { 246 throw new IOException ("ByteArrayParserInputStream.unread(int): " + 247 "cannot unread " + len + 248 " bytes at buffer position " + datapos); 249 } 250 datapos -= len; 251 System.arraycopy(buffer, off, data, datapos, len); 252 } 253 254 259 public int read() 260 { 261 try 262 { 263 return (data[datapos++] + 0x100) & 0xff; 265 } 266 catch (ArrayIndexOutOfBoundsException ex) 267 { 268 datapos = datalen; 272 return -1; 273 } 274 } 275 276 283 public int read(byte[] buffer) 284 { 285 return localRead(buffer, 0, buffer.length); 286 } 287 288 297 public int read(byte[] buffer, int off, int len) 298 { 299 if (len <= 0 || off >= buffer.length) 300 { 301 return 0; 302 } 303 if (off < 0) 304 { 305 off = 0; 306 } 307 if (len > buffer.length) 308 { 309 len = buffer.length; 310 } 311 return localRead(buffer, off, len); 312 } 313 314 315 325 public int localRead(byte[] buffer, int off, int len) 326 { 327 if (len == 0) 328 { 329 return 0; } 331 else if (datapos >= datalen) 332 { 333 return -1; 334 } 335 else 336 { 337 int newpos = datapos + len; 338 if (newpos > datalen) 339 { 340 newpos = datalen; 341 len = newpos - datapos; 342 } 343 System.arraycopy(data, datapos, buffer, off, len); 344 datapos = newpos; 345 return len; 346 } 347 } 348 349 360 public long skip(long num) 361 { 362 if (num <= 0) 363 { 364 return 0; 365 } 366 else 367 { 368 long newpos = datapos + num; 369 if (newpos >= datalen) 370 { 371 num = datalen - datapos; 372 datapos = datalen; 373 } 374 else 375 { 376 datapos = (int)newpos; 377 } 378 return num; 379 } 380 } 381 382 389 public int seek(int newpos) 390 { 391 if (newpos < 0) 392 { 393 newpos = 0; 394 } 395 else if (newpos > datalen) 396 { 397 newpos = datalen; 398 } 399 int oldpos = pos; 400 pos = newpos; 401 return oldpos; 402 } 403 404 } 405 | Popular Tags |