KickJava   Java API By Example, From Geeks To Geeks.

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


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 RandomAccess<br>
39  *
40  * @author Andrei Kouzentsov
41  */

42 public class RandomAccessByteArray extends AbstractRandomAccess {
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 java.io.IOException
54      */

55     public RandomAccessByteArray(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 java.io.IOException
65      */

66     public RandomAccessByteArray(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     /**
98      * Sets the length of byte array.
99      * no array copying is performed.
100      * @param newLength The desired length of the file
101      * @exception java.io.IOException if <code>length</code> is more then length of array minus <code>offset</code>
102      */

103     public void setLength(long newLength) throws IOException JavaDoc {
104         if(newLength < 0) {
105             throw new ArrayIndexOutOfBoundsException JavaDoc("" + newLength);
106         }
107         if(newLength == length) {
108             return;
109         }
110         else if(newLength < buf.length - offset) {
111             length = (int)newLength;
112             return;
113         }
114         else {
115 // byte[] tmp = new byte[(int)newLength];
116
// int length = Math.min(tmp.length, length);
117
// System.arraycopy(buf, offset, tmp, 0, length);
118
throw new IOException JavaDoc("length too big");
119         }
120     }
121
122
123     public int read(byte[] b) throws IOException JavaDoc {
124         return read(b, 0, b.length);
125     }
126
127     public int read(byte[] b, int off, int length) throws IOException JavaDoc {
128         int len = Math.min(length, this.length - (fp + offset));
129         if(len <= 0) {
130             return -1;
131         }
132         System.arraycopy(buf, fp + offset, b, off, len);
133         fp += len;
134         return len;
135     }
136
137
138     /**
139      * Writes max <code>b.length</code> bytes from the specified byte array
140      * to this array, starting at the current array pointer.
141      *
142      * This method doesn't write beyond array bounds,
143      * but <code>off</code> and <code>length</code> are not checked.
144      *
145      * @param b the data.
146      */

147     public void write(byte[] b) throws IOException JavaDoc {
148         write(b, 0, b.length);
149     }
150
151     /**
152      * Writes <code>len</code> bytes from the specified byte array
153      * starting at offset <code>off</code> to this byte array.<br>
154      *
155      * This method doesn't write beyond array bounds,
156      * but <code>off</code> and <code>length</code> are not checked
157      * and no exception is thrown.
158      *
159      * @param b the data.
160      * @param off the start offset in the data.
161      * @param length the number of bytes to write
162      */

163     public void write(byte[] b, int off, int length) {
164         int len = Math.min(length, this.length - (fp + offset));
165         System.arraycopy(b, off, buf, fp + offset, len);
166         fp += len;
167     }
168
169     /**
170      * Writes the specified byte to this array. The write starts at the current array pointer.
171      *
172      * @param b the <code>byte</code> to be written.
173      */

174     public void write(int b) throws IOException JavaDoc {
175         buf[offset + fp++] = (byte)b;
176     }
177
178     /**
179      * does nothing
180      */

181     public void close() {
182     }
183
184     public int getOffset() {
185         return offset;
186     }
187
188     public void setOffset(int offset) {
189         this.offset = offset;
190     }
191
192     public int skip(int n) throws IOException JavaDoc {
193         return skipBytes(n);
194     }
195
196     public long getFilePointer() throws IOException JavaDoc {
197         return fp + offset;
198     }
199
200     /**
201      * Sets the array-pointer offset, measured from the beginning of this byte array, at which the next read or write
202      * occurs. The offset may NOT be set beyond the end of the byte array.
203      *
204      * @param pos the offset position, measured in bytes from the beginning of the byte array, at which
205      * to set the array pointer.
206      */

207     public void seek(long pos) {
208         if((pos > length + offset) || pos < offset) {
209             throw new ArrayIndexOutOfBoundsException JavaDoc("" + pos);
210         }
211         this.fp = (int)pos;
212     }
213
214     /**
215      * Returns the length of this byte array.
216      *
217      * @return the length of this byte array, measured in bytes.
218      */

219     public long length() {
220         return length;
221     }
222 }
223
Popular Tags