KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.imagero.uio.buffer.Buffer;
35 import com.imagero.uio.buffer.BufferManager;
36 import com.imagero.uio.buffer.DefaultBufferManager;
37
38 import java.io.EOFException JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Represent (multiple) Buffer as RandomAccessRO
43  * <br>
44  *
45  * @author Andrei Kouzentsov
46  */

47 public class RandomAccessBufferRO extends AbstractRandomAccessRO {
48
49     protected BufferManager bufferManager;
50     int dataIndex;
51     int fp;
52     byte[] buf;
53
54     /**
55      * create new RABuffer
56      *
57      * @throws IOException
58      */

59     public RandomAccessBufferRO(Buffer[] ds, int byteOrder) throws IOException JavaDoc {
60         this(new DefaultBufferManager(ds), byteOrder);
61     }
62
63     /**
64      * create new RABuffer
65      *
66      * @param sourceManager
67      *
68      * @throws IOException
69      */

70     public RandomAccessBufferRO(BufferManager sourceManager, int byteOrder) throws IOException JavaDoc {
71         this.bufferManager = sourceManager;
72         this.buf = sourceManager.getData(0);
73         _setByteOrder(byteOrder);
74     }
75
76     public BufferManager getBufferManager() {
77         return bufferManager;
78     }
79
80     /**
81      * Reads a byte of data from this byte array. The byte is returned as an integer in the range 0 to 255
82      * (<code>0x00-0x0ff</code>).
83      *
84      * @return the next byte of data, or <code>-1</code> if the end of the file has been reached.
85      */

86     public int read() throws IOException JavaDoc {
87         if(fp < buf.length) {
88             return buf[fp++] & 0xFF;
89         }
90         else {
91             nextArray();
92             if(buf == null || buf.length == 0) {
93                 return -1;
94             }
95             return buf[fp++] & 0xFF;
96         }
97     }
98
99     protected boolean nextArray() throws IOException JavaDoc {
100         //Sys.out.println("nextArray: " + totalRead);
101
try {
102             this.buf = bufferManager.getData(++dataIndex);
103             this.fp = 0;
104             if(buf.length == 0) {
105                 return false;
106             }
107             return true;
108         }
109         catch(IOException JavaDoc ex) {
110             this.buf = null;
111             this.fp = 0;
112 // return false;
113
throw ex;
114         }
115     }
116
117     public long getFilePointer() throws IOException JavaDoc {
118         return bufferManager.getDataStart(dataIndex) + fp;
119     }
120
121     /**
122      * Sets the pointer offset, measured in bytes from the begin of the data,
123      * at which the next read or write occurs.
124      *
125      * @param pos the offset position, measured in bytes from the begin of the data, at which
126      * to set the pointer.
127      */

128     public void seek(long pos) throws IOException JavaDoc {
129         if(pos < 0) {
130             throw new IOException JavaDoc("" + pos);
131         }
132
133         this.dataIndex = bufferManager.getIndex((int) pos);
134         this.buf = bufferManager.getData(dataIndex);
135         this.fp = (int) (pos - bufferManager.getDataStart(dataIndex));
136     }
137
138     /**
139      * Returns the data length (please note, that real length is not always known)
140      *
141      * @return the data length, measured in bytes.
142      */

143     public long length() {
144         return bufferManager.getLength();
145     }
146
147     public int read(byte[] b) throws IOException JavaDoc {
148         return read(b, 0, b.length);
149     }
150
151     public int read(byte[] b, int off, int length) throws IOException JavaDoc {
152         int read = 0;
153         int _off = off;
154         int _length = length;
155
156         while(read < _length) {
157             int len = readBytes(b, _off, _length);
158             if(len == -1) {
159                 if(!nextArray()) {
160                     return read > 0 ? read : -1;
161                 }
162             }
163             else {
164                 read += len;
165                 fp += len;
166                 _off += len;
167                 _length -= len;
168             }
169         }
170
171         return read;
172     }
173
174     private int readBytes(byte[] b, int off, int length) {
175         if(buf == null) {
176             return -1;
177         }
178         int len = Math.min(length, buf.length - fp);
179         if(len <= 0) {
180             return -1;
181         }
182         System.arraycopy(buf, fp, b, off, len);
183         return len;
184     }
185
186     public void close() {
187         bufferManager.close();
188     }
189
190     public int skip(int n) throws IOException JavaDoc {
191         return skipBytes(n);
192     }
193
194     protected int _read() throws IOException JavaDoc {
195         int a = read();
196         if(a < 0) {
197             throw new EOFException JavaDoc();
198         }
199         return a;
200     }
201 }
202
Popular Tags