Revision: 6991
Author: victormote
Date: 2006-03-25 09:04:25 -0800 (Sat, 25 Mar 2006)
ViewCVS: http://svn.sourceforge.net/foray/?rev=6991&view=rev
Log Message:
-----------
Add RandomReader implementation that uses a byte array instead of a File.
Added Paths:
-----------
trunk/foray/foray-common/src/java/org/foray/common/RandomReaderArray.java
Added: trunk/foray/foray-common/src/java/org/foray/common/RandomReaderArray.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/RandomReaderArray.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/RandomReaderArray.java 2006-03-25 17:04:25 UTC (rev 6991)
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2006 The FOray Project.
+ * http://www.foray.org
+ *
+ * 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.
+ *
+ * This work is in part derived from the following work(s), used with the
+ * permission of the licensor:
+ * Apache FOP, licensed by the Apache Software Foundation
+ *
+ */
+
+/* $Id$ */
+
+package org.foray.common;
+
+import java.io.DataInputStream;
+import java.io.EOFException;
+import java.io.IOException;
+
+/**
+ * Implementation of RandomInput that uses a RandomAccessFile to do the
+ * io work. Class has package-visibility only as it should only be instantiated
+ * and used by RandomReader.
+ */
+class RandomReaderArray implements RandomInput {
+
+ private byte[] byteArray = null;
+ private int arrayIndex = 0;
+
+ /**
+ * Constructor.
+ */
+ protected RandomReaderArray(byte[] byteArray) {
+ this.byteArray = byteArray;
+ }
+
+ public void seek(long offset) throws IOException {
+ arrayIndex = (int) offset;
+ }
+
+ public int skipBytes(int add) throws IOException {
+ return arrayIndex += add;
+ }
+
+ public long length() throws IOException {
+ return this.byteArray.length;
+ }
+
+ public byte readByte() throws IOException {
+ return this.byteArray[arrayIndex];
+ }
+
+ public final int readUnsignedByte() throws IOException {
+ if (this.arrayIndex >= this.byteArray.length) {
+ throw new EOFException();
+ }
+ char c = (char) this.byteArray[this.arrayIndex];
+ this.arrayIndex ++;
+ return c;
+ }
+
+ public final short readShort() throws IOException {
+ int byte1 = readUnsignedByte();
+ int byte2 = readUnsignedByte();
+ return (short)((byte1 << 8) + (byte2 << 0));
+ }
+
+ public final int readUnsignedShort() throws IOException {
+ int byte1 = readUnsignedByte();
+ int byte2 = readUnsignedByte();
+ return (byte1 << 8) + (byte2 << 0);
+ }
+
+ public final double readDouble() throws IOException {
+ return Double.longBitsToDouble(readLong());
+ }
+
+ public final boolean readBoolean() throws IOException {
+ int ch = readUnsignedByte();
+ return (ch != 0);
+ }
+
+ 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 {
+ boolean eofDetected = false;
+ if (this.arrayIndex + len >= this.byteArray.length) {
+ eofDetected = true;
+ len = this.byteArray.length - this.arrayIndex;
+ }
+ System.arraycopy(this.byteArray, this.arrayIndex, b, off, len);
+ this.arrayIndex += len;
+ if (eofDetected) {
+ throw new EOFException();
+ }
+ }
+
+ public final long readLong() throws IOException {
+ return ((long) (readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
+ }
+
+ public final float readFloat() throws IOException {
+ return Float.intBitsToFloat(readInt());
+ }
+
+ public final char readChar() throws IOException {
+ int byte1 = this.readUnsignedByte();
+ int byte2 = this.readUnsignedByte();
+ return (char)((byte1 << 8) + (byte2 << 0));
+ }
+
+ public final String readUTF() throws IOException {
+ return DataInputStream.readUTF(this);
+ }
+
+ public final int readInt() throws IOException {
+ int byte1 = this.readUnsignedByte();
+ int byte2 = this.readUnsignedByte();
+ int byte3 = this.readUnsignedByte();
+ int byte4 = this.readUnsignedByte();
+ return ((byte1 << 24) + (byte2 << 16) + (byte3 << 8) + (byte4 << 0));
+ }
+
+ public final String readLine() throws IOException {
+ StringBuffer input = new StringBuffer();
+ int byteRead = -1;
+ boolean eol = false;
+
+ while (!eol) {
+ switch (byteRead = this.readUnsignedByte()) {
+ case -1:
+ case '\n': {
+ eol = true;
+ break;
+ }
+ case '\r': {
+ eol = true;
+ long cur = getFilePointer();
+ if ((this.readUnsignedByte()) != '\n') {
+ seek(cur);
+ }
+ break;
+ }
+ default: {
+ input.append((char)byteRead);
+ break;
+ }
+ }
+ }
+ if ((byteRead == -1) && (input.length() == 0)) {
+ return null;
+ }
+ return input.toString();
+ }
+
+ public long getFilePointer() throws IOException {
+ return this.arrayIndex;
+ }
+
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/RandomReaderArray.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|