KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > RandomAccessByteArrayRO


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package com.imagero.uio;
33
34 import java.io.EOFException JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 /**
38  * Makes possible to access byte array as RandomAccessRO<br>
39  *
40  * @author Andrei Kouzentsov
41  */

42 public class RandomAccessByteArrayRO extends AbstractRandomAccessRO {
43
44     int fp;
45
46     byte[] buf;
47     int length;
48     int offset;
49
50     /**
51      * create new RandomAccessByteArray
52      * @param data byte array
53      * @throws IOException
54      */

55     public RandomAccessByteArrayRO(byte[] data, int byteOrder) throws IOException JavaDoc {
56         this(data, 0, data.length, byteOrder);
57     }
58
59     /**
60      * create new RandomAccessByteArray
61      * @param data byte array
62      * @param off index of the first byte
63      * @param length number of bytes
64      * @throws IOException
65      */

66     public RandomAccessByteArrayRO(byte[] data, int off, int length, int byteOrder) throws IOException JavaDoc {
67         this.buf = data;
68         this.length = length;
69         this.offset = off;
70         _setByteOrder(byteOrder);
71     }
72
73     /**
74      * Reads a byte of data from this byte array. The byte is returned as an integer in the range 0 to 255
75      * (<code>0x00-0x0ff</code>).
76      *
77      * @return the next byte of data, or <code>-1</code> if the end of the file has been reached.
78      */

79     public int read() {
80         if(fp < length + offset) {
81             return buf[offset + fp++] & 0xFF;
82         }
83         else {
84             return -1;
85         }
86     }
87
88     protected int _read() throws EOFException JavaDoc {
89         if(fp < length + offset) {
90             return buf[offset + fp++] & 0xFF;
91         }
92         else {
93             throw new EOFException JavaDoc();
94         }
95     }
96
97     public int read(byte[] b) throws IOException JavaDoc {
98         return read(b, 0, b.length);
99     }
100
101     public int read(byte[] b, int off, int length) throws IOException JavaDoc {
102         int len = Math.min(length, this.length - (fp + offset));
103         if(len <= 0) {
104             return -1;
105         }
106         System.arraycopy(buf, fp + offset, b, off, len);
107         fp += len;
108         return len;
109     }
110
111     /**
112      * does nothing
113      */

114     public void close() {
115     }
116
117     protected int getOffset() {
118         return offset;
119     }
120
121     protected void setOffset(int offset) {
122         this.offset = offset;
123     }
124
125     public int skip(int n) throws IOException JavaDoc {
126         return skipBytes(n);
127     }
128
129     public long getFilePointer() throws IOException JavaDoc {
130         return fp + offset;
131     }
132
133     /**
134      * Sets the array-pointer offset, measured from the beginning of this byte array, at which the next read or write
135      * occurs. The offset may NOT be set beyond the end of the byte array.
136      *
137      * @param pos the offset position, measured in bytes from the beginning of the byte array, at which
138      * to set the array pointer.
139      */

140     public void seek(long pos) {
141         if((pos > length + offset) || pos < offset) {
142             throw new ArrayIndexOutOfBoundsException JavaDoc("" + pos);
143         }
144         this.fp = (int)pos;
145     }
146
147     /**
148      * Returns the length of this byte array.
149      *
150      * @return the length of this byte array, measured in bytes.
151      */

152     public long length() {
153         return length;
154     }
155 }
156
Popular Tags