1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.EOFException ; 28 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 31 import org.apache.derby.iapi.services.io.LimitObjectInput; 32 import org.apache.derby.iapi.services.io.ErrorObjectInput; 33 34 import java.io.UTFDataFormatException ; 35 36 41 public final class ArrayInputStream extends InputStream implements LimitObjectInput { 42 43 private byte[] pageData; 44 45 private int start; 46 private int end; private int position; 48 49 public ArrayInputStream() { 50 this(null); 51 } 52 53 private ErrorObjectInput oi; 54 55 public ArrayInputStream(byte[] data) { 56 super(); 57 setData(data); 58 oi = new org.apache.derby.iapi.services.io.FormatIdInputStream(this); 59 } 60 61 public ArrayInputStream(byte[] data, int offset, int length) throws IOException { 62 this(data); 63 setLimit(offset, length); 64 } 65 66 69 70 73 public void setData(byte[] data) { 74 pageData = data; 75 clearLimit(); 76 } 77 78 public void setData(byte[] data, int offset, int length) throws IOException { 79 pageData = data; 80 setLimit(offset, length); 81 } 82 83 87 public byte[] getData() 88 { 89 return pageData; 90 } 91 92 95 96 public int read() throws IOException { 97 if (position == end) 98 return -1; 100 return pageData[position++] & 0xff ; 101 102 } 103 104 public int read(byte b[], int off, int len) throws IOException { 105 106 if ((position + len) > end) { 107 108 len = end - position; 109 110 if (len == 0) { 111 return -1; } 113 } 114 115 System.arraycopy(pageData, position, b, off, len); 116 position += len; 117 return len; 118 } 119 120 public long skip(long count) throws IOException { 121 122 if ((position + count) > end) { 123 124 count = end - position; 125 126 if (count == 0) 127 return 0; } 129 130 position += count; 131 132 return count; 133 134 } 135 136 public int getPosition() { 137 return position; 138 } 139 140 public final void setPosition(int newPosition) 141 throws IOException { 142 143 if ((newPosition >= start) && (newPosition < end)) 144 position = newPosition; 145 else 146 throw new EOFException (); 147 } 148 149 public int available() throws IOException { 150 151 return end - position; 152 } 153 154 155 160 public int setLimit(int offset, int length) throws IOException { 161 162 if ((offset < 0) || (length < 0)) { 163 start = end = position = 0; 164 throw new EOFException (); 165 } 166 167 start = offset; 168 end = offset + length; 169 170 if (end > pageData.length) { 171 start = end = position = 0; 172 throw new EOFException (); 173 } 174 175 position = start; 176 177 return length; 178 } 179 180 183 184 public final void setLimit(int length) throws IOException { 185 186 start = position; 187 end = position + length; 188 189 if (end <= pageData.length) 190 { 191 return; 192 } 193 else 194 { 195 start = end = position = 0; 196 throw new EOFException (); 197 } 198 } 199 200 205 public final int clearLimit() { 206 207 if (pageData != null) { 208 start = 0; 209 int remainingBytes = end - position; 210 end = pageData.length; 211 return remainingBytes; 212 } else { 213 start = end = position = 0; 214 return 0; 215 } 216 } 217 218 221 222 public final void readFully(byte b[]) throws IOException { 223 readFully(b, 0, b.length); 224 } 225 226 public final void readFully(byte b[], int off, int len) throws IOException { 227 228 if ((position + len) > end) { 229 230 throw new EOFException (); 231 } 232 233 System.arraycopy(pageData, position, b, off, len); 234 position += len; 235 } 236 237 public final int skipBytes(int n) throws IOException { 238 if ((position + n) > end) { 239 240 throw new EOFException (); 241 } 242 position += n; 243 return n; 244 } 245 246 public final boolean readBoolean() throws IOException { 247 if (position == end) 248 throw new EOFException (); 250 return pageData[position++] != 0; 251 } 252 253 public final byte readByte() throws IOException { 254 if (position == end) 255 throw new EOFException (); 257 return pageData[position++]; 258 } 259 260 public final int readUnsignedByte() throws IOException { 261 if (position == end) 262 throw new EOFException (); 264 return pageData[position++] & 0xff ; 265 } 266 267 public final short readShort() throws IOException { 268 269 int pos = position; 270 byte[] data = pageData; 271 272 if (pos >= (end - 1)) 273 throw new EOFException (); 275 int s = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); 276 277 position = pos; 278 279 return (short) s; 280 } 281 282 public final int readUnsignedShort() throws IOException { 283 int pos = position; 284 byte[] data = pageData; 285 286 if (pos >= (end - 1)) 287 throw new EOFException (); 289 int us = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); 290 291 position = pos; 292 293 return us; 294 } 295 296 public final char readChar() throws IOException { 297 int pos = position; 298 byte[] data = pageData; 299 300 if (pos >= (end -1)) 301 throw new EOFException (); 303 int c = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); 304 305 position = pos; 306 307 return (char) c; 308 } 309 310 public final int readInt() throws IOException { 311 312 int pos = position; 313 byte[] data = pageData; 314 315 if (pos >= (end - 3)) 316 throw new EOFException (); 318 319 320 int i = ((data[pos++] & 0xff) << 24) | 321 ((data[pos++] & 0xff) << 16) | 322 ((data[pos++] & 0xff) << 8) | 323 ((data[pos++] & 0xff) ); 324 325 position = pos; 326 327 return i; 328 } 329 330 public final long readLong() throws IOException { 331 int pos = position; 332 byte[] data = pageData; 333 334 if (pos >= (end - 7)) 335 throw new EOFException (); 337 long l = 338 (((long) (data[pos++] & 0xff)) << 56) | 339 (((long) (data[pos++] & 0xff)) << 48) | 340 (((long) (data[pos++] & 0xff)) << 40) | 341 (((long) (data[pos++] & 0xff)) << 32) | 342 (((long) (data[pos++] & 0xff)) << 24) | 343 (((long) (data[pos++] & 0xff)) << 16) | 344 (((long) (data[pos++] & 0xff)) << 8) | 345 (((long) (data[pos++] & 0xff)) ); 346 347 position = pos; 348 349 return l; 350 } 351 352 public final float readFloat() throws IOException { 353 return Float.intBitsToFloat(readInt()); 354 } 355 356 public final double readDouble() throws IOException { 357 return Double.longBitsToDouble(readLong()); 358 } 359 360 public final String readLine() throws IOException { 361 return oi.readLine(); 362 } 363 public final String readUTF() throws IOException { 364 return oi.readUTF(); 365 } 366 367 398 public final int readCloudscapeUTF(char[][] rawData_array) 399 throws IOException 400 { 401 byte[] data = pageData; 403 int end_pos = end; 404 int pos = position; 405 406 408 int utflen; 409 if (pos + 1 < end_pos) 410 { 411 utflen = (((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff)); 412 } 413 else 414 { 415 throw new EOFException (); } 417 418 425 426 int requiredLength; 431 if (utflen != 0) 432 { 433 436 if (utflen <= (end_pos - pos)) 437 { 438 requiredLength = utflen; 439 } 440 else 441 { 442 throw new EOFException (); 443 } 444 } 445 else 446 { 447 449 requiredLength = (end_pos - pos); 450 } 451 452 char[] str = rawData_array[0]; 461 if ((str == null) || (requiredLength > str.length)) 462 { 463 str = new char[requiredLength]; 464 rawData_array[0] = str; 465 } 466 467 end_pos = pos + requiredLength; 468 int strlen = 0; 469 470 while (pos < end_pos) 471 { 472 int char1 = (data[pos++] & 0xff); 473 474 494 int char2, char3; 495 if ((char1 & 0x80) == 0x00) 496 { 497 str[strlen++] = (char) char1; 499 } 500 else if ((char1 & 0x60) == 0x40) { 502 if (pos >= end_pos) 504 throw new UTFDataFormatException (); 505 506 char2 = (data[pos++] & 0xff); 507 508 if ((char2 & 0xC0) != 0x80) 509 throw new UTFDataFormatException (); 510 511 str[strlen++] = (char)(((char1 & 0x1F) << 6) | (char2 & 0x3F)); 512 } 513 else if ((char1 & 0x70) == 0x60) { 515 517 if (pos + 1 >= end_pos) 519 throw new UTFDataFormatException (); 520 521 char2 = (data[pos++] & 0xff); 522 char3 = (data[pos++] & 0xff); 523 524 if ((char1 == 0xE0) && 525 (char2 == 0) && 526 (char3 == 0) && 527 (utflen == 0)) 528 { 529 break; 533 } 534 else if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 535 { 536 throw new UTFDataFormatException (); 537 } 538 else 539 { 540 str[strlen++] = (char) 541 (((char1 & 0x0F) << 12) | 542 ((char2 & 0x3F) << 6) | 543 ((char3 & 0x3F) << 0)); 544 } 545 } 546 else 547 { 548 throw new UTFDataFormatException (); 549 } 550 551 } 552 553 position = pos; 555 556 return(strlen); 557 } 558 559 579 public final int readCompressedInt() 580 throws IOException 581 { 582 int pos = position; 583 byte[] data = pageData; 584 585 try 586 { 587 int value = data[pos++]; 588 589 if ((value & ~0x3f) == 0) 590 { 591 } 595 else if ((value & 0x80) == 0) 596 { 597 599 if (SanityManager.DEBUG) 600 { 601 SanityManager.ASSERT((value & 0x40) == 0x40); 602 } 603 604 610 value = 611 (((value & 0x3f) << 8) | (data[pos++] & 0xff)); 612 } 613 else 614 { 615 617 if (SanityManager.DEBUG) 618 { 619 SanityManager.ASSERT((value & 0x80) == 0x80); 620 } 621 622 629 value = 630 ((value & 0x7f) << 24) | 631 ((data[pos++] & 0xff) << 16) | 632 ((data[pos++] & 0xff) << 8) | 633 ((data[pos++] & 0xff) ); 634 } 635 636 position = pos; 637 638 return(value); 639 } 640 catch (java.lang.ArrayIndexOutOfBoundsException ex) 641 { 642 throw new EOFException (); } 644 645 } 646 647 671 public final long readCompressedLong() 672 throws IOException 673 { 674 try 675 { 676 int pos = position; 678 byte[] data = pageData; 679 680 int int_value = data[pos++]; 682 683 long long_value; 685 686 if ((int_value & ~0x3f) == 0) 687 { 688 690 long_value = ((int_value << 8) | (data[pos++] & 0xff)); 694 } 695 else if ((int_value & 0x80) == 0) 696 { 697 699 702 long_value = 703 ((int_value & 0x3f) << 24) | 704 ((data[pos++] & 0xff) << 16) | 705 ((data[pos++] & 0xff) << 8) | 706 ((data[pos++] & 0xff) ); 707 } 708 else 709 { 710 717 long_value = 719 (((long) (int_value & 0x7f)) << 56) | 720 (((long) (data[pos++] & 0xff)) << 48) | 721 (((long) (data[pos++] & 0xff)) << 40) | 722 (((long) (data[pos++] & 0xff)) << 32) | 723 (((long) (data[pos++] & 0xff)) << 24) | 724 (((long) (data[pos++] & 0xff)) << 16) | 725 (((long) (data[pos++] & 0xff)) << 8) | 726 (((long) (data[pos++] & 0xff)) ); 727 } 728 729 position = pos; 730 731 return(long_value); 732 } 733 catch (java.lang.ArrayIndexOutOfBoundsException ex) 734 { 735 737 throw new EOFException (); } 739 } 740 741 public Object readObject() throws ClassNotFoundException , IOException { 742 return oi.readObject(); 743 } 744 745 public String getErrorInfo() { 746 return oi.getErrorInfo(); 747 } 748 749 public Exception getNestedException() { 750 return oi.getNestedException(); 751 } 752 } 753 | Popular Tags |