📄 arrayinputstream.java
字号:
/* Derby - Class org.apache.derby.iapi.services.io.ArrayInputStream Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.iapi.services.io;import java.io.InputStream;import java.io.IOException;import java.io.ObjectInput;import java.io.EOFException;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.LimitObjectInput;import org.apache.derby.iapi.services.io.ErrorObjectInput;import java.io.UTFDataFormatException;/** An InputStream that allows reading from an array of bytes. The array of bytes that is read from can be changed without having to create a new instance of this class.*/public final class ArrayInputStream extends InputStream implements LimitObjectInput { private byte[] pageData; private int start; private int end; // exclusive private int position; public ArrayInputStream() { this(null); } private ErrorObjectInput oi; public ArrayInputStream(byte[] data) { super(); setData(data); oi = new org.apache.derby.iapi.services.io.FormatIdInputStream(this); } public ArrayInputStream(byte[] data, int offset, int length) throws IOException { this(data); setLimit(offset, length); } /* ** Public methods */ /** Set the array of bytes to be read. */ public void setData(byte[] data) { pageData = data; clearLimit(); } public void setData(byte[] data, int offset, int length) throws IOException { pageData = data; setLimit(offset, length); } /** Return a reference to the array of bytes this stream is going to read from so that caller may load it with stuff */ public byte[] getData() { return pageData; } /* ** Methods of InputStream */ public int read() throws IOException { if (position == end) return -1; // end of file return pageData[position++] & 0xff ; } public int read(byte b[], int off, int len) throws IOException { if ((position + len) > end) { len = end - position; if (len == 0) { return -1; // end of file } } System.arraycopy(pageData, position, b, off, len); position += len; return len; } public long skip(long count) throws IOException { if ((position + count) > end) { count = end - position; if (count == 0) return 0; // end of file } position += count; return count; } public int getPosition() { return position; } public final void setPosition(int newPosition) throws IOException { if ((newPosition >= start) && (newPosition < end)) position = newPosition; else throw new EOFException(); } public int available() throws IOException { return end - position; } /** A setLimit which also sets the position to be offset. @exception IOException limit is out of range */ public int setLimit(int offset, int length) throws IOException { if ((offset < 0) || (length < 0)) { start = end = position = 0; throw new EOFException(); } start = offset; end = offset + length; if (end > pageData.length) { start = end = position = 0; throw new EOFException(); } position = start; return length; } /* ** Methods of Limit */ public final void setLimit(int length) throws IOException { start = position; end = position + length; if (end <= pageData.length) { return; } else { start = end = position = 0; throw new EOFException(); } } /** Clears the limit by setting the limit to be the entire byte array. @see Limit#clearLimit */ public final int clearLimit() { if (pageData != null) { start = 0; int remainingBytes = end - position; end = pageData.length; return remainingBytes; } else { start = end = position = 0; return 0; } } /* ** Methods of DataInput */ public final void readFully(byte b[]) throws IOException { readFully(b, 0, b.length); } public final void readFully(byte b[], int off, int len) throws IOException { if ((position + len) > end) { throw new EOFException(); } System.arraycopy(pageData, position, b, off, len); position += len; } public final int skipBytes(int n) throws IOException { if ((position + n) > end) { throw new EOFException(); } position += n; return n; } public final boolean readBoolean() throws IOException { if (position == end) throw new EOFException(); // end of file return pageData[position++] != 0; } public final byte readByte() throws IOException { if (position == end) throw new EOFException(); // end of file return pageData[position++]; } public final int readUnsignedByte() throws IOException { if (position == end) throw new EOFException(); // end of file return pageData[position++] & 0xff ; } public final short readShort() throws IOException { int pos = position; byte[] data = pageData; if (pos >= (end - 1)) throw new EOFException(); // end of file int s = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); position = pos; return (short) s; } public final int readUnsignedShort() throws IOException { int pos = position; byte[] data = pageData; if (pos >= (end - 1)) throw new EOFException(); // end of file int us = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); position = pos; return us; } public final char readChar() throws IOException { int pos = position; byte[] data = pageData; if (pos >= (end -1)) throw new EOFException(); // end of file int c = ((data[pos++] & 0xff) << 8) | (data[pos++] & 0xff); position = pos; return (char) c; } public final int readInt() throws IOException { int pos = position; byte[] data = pageData; if (pos >= (end - 3)) throw new EOFException(); // end of file int i = ((data[pos++] & 0xff) << 24) | ((data[pos++] & 0xff) << 16) | ((data[pos++] & 0xff) << 8) | ((data[pos++] & 0xff) ); position = pos; return i; } public final long readLong() throws IOException { int pos = position; byte[] data = pageData; if (pos >= (end - 7)) throw new EOFException(); // end of file long l = (((long) (data[pos++] & 0xff)) << 56) | (((long) (data[pos++] & 0xff)) << 48) | (((long) (data[pos++] & 0xff)) << 40) | (((long) (data[pos++] & 0xff)) << 32) | (((long) (data[pos++] & 0xff)) << 24) | (((long) (data[pos++] & 0xff)) << 16) | (((long) (data[pos++] & 0xff)) << 8) | (((long) (data[pos++] & 0xff)) ); position = pos; return l; } public final float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); } public final double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); } public final String readLine() throws IOException { return oi.readLine(); } public final String readUTF() throws IOException { return oi.readUTF(); } /** * read in a cloudscape UTF formated string into a char[]. * <p> * This routine inline's the code to read a UTF format string from a * byte[] array (pageData), into a char[] array. The string will * be read into the char[] array passed into this routine through * rawData_array[0] if it is big enough. If it is not big enough * a new char[] will be alocated and returned to the caller by putting * it into rawData_array[0]. * <p> * To see detailed description of the cloudscape UTF format see
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -