KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > io > MSInputStream


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.io;
33
34 import com.imagero.uio.Sys;
35 import com.imagero.uio.buffer.Buffer;
36 import com.imagero.uio.buffer.BufferManager;
37 import com.imagero.uio.buffer.ByteBuffer;
38 import com.imagero.uio.buffer.DefaultBufferManager;
39
40 import java.io.EOFException JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 /**
47  * MSInputStream.java <br>
48  * MS = MultipleSource
49  *
50  * @author Kouznetsov Andrei
51  */

52 public class MSInputStream extends InputStream JavaDoc {
53
54     BufferManager bufferManager;
55
56     int vpos;
57     int vcount;
58     int totalRead;
59     int totalCount;
60
61     protected byte buf[];
62
63     protected int pos;
64     protected int count;
65     protected int mark = 0;
66
67     /**
68      * create InputStream over multiple byte arrays
69      *
70      * @param v Vector that contains some byte arrays
71      */

72     public MSInputStream(Vector JavaDoc v) {
73         Buffer [] ds = new Buffer[v.size()];
74         for (int i = 0; i < ds.length; i++) {
75             ds[i] = new ByteBuffer((byte[]) v.elementAt(i));
76         }
77         this.bufferManager = new DefaultBufferManager(ds);
78
79         this.vcount = v.size();
80         this.totalCount = countBytes();
81         nextArray();
82     }
83
84     /**
85      * create InputStream over multiple Buffers
86      *
87      * @param ds Buffer array
88      */

89     public MSInputStream(Buffer[] ds) {
90         this(new DefaultBufferManager(ds));
91     }
92
93     public MSInputStream(BufferManager sourceManager) {
94         this.bufferManager = sourceManager;
95         this.vcount = sourceManager.getCount();
96         this.totalCount = countBytes();
97         nextArray();
98     }
99
100     protected int countBytes() {
101         int count = 0;
102         for(int i = 0; i < bufferManager.getCount(); i++) {
103             count += bufferManager.getDataLength(i);
104         }
105         return count;
106     }
107
108     protected void nextArray() {
109         //Sys.out.println("nextArray: " + totalRead);
110
try {
111             this.buf = bufferManager.getData(vpos++);
112             this.pos = 0;
113             this.count = buf.length;
114         }
115         catch(Exception JavaDoc ex) {
116             //ex.printStackTrace();
117
this.buf = null;
118             this.pos = 0;
119             this.count = 0;
120         }
121     }
122
123     public int read(byte b[], int off, int len) throws IOException JavaDoc {
124         if(totalRead >= totalCount) { //???
125
throw new EOFException JavaDoc();
126         }
127
128         int i = 0;
129         for(; i < len; i++) {
130             int c = read();
131             if(c == -1) {
132                 break;
133             }
134             b[off + i] = (byte) c;
135         }
136         return i;
137     }
138
139     public int read() throws IOException JavaDoc {
140         if(totalRead < totalCount) {
141             if(pos == count) {
142                 nextArray();
143                 if(buf == null) {
144                     return -1;
145                 }
146                 //Sys.out.println("pos: " + pos);
147
}
148             totalRead++;
149             return buf[pos++] & 0xFF;
150         }
151         return -1;
152     }
153
154     public static void printHex(int value) {
155         value = value & 0xFF;
156         String JavaDoc s = Integer.toHexString(value);
157         if(s.length() == 1) {
158             Sys.out.print("0");
159         }
160         Sys.out.print(s);
161         Sys.out.print(" ");
162     }
163
164     public synchronized long skip(long n) {
165         int skipped = 0;
166         while(n + pos > count) {
167             int skp = count - pos;
168
169             n -= skp;
170             skipped += skp;
171             totalRead += skp;
172
173             nextArray();
174             if(buf == null) {
175                 return skipped;
176             }
177         }
178         return skipped;
179     }
180
181     public int available() {
182         return totalCount - totalRead;
183     }
184
185     public void mark(int readAheadLimit) {
186         mark = totalRead;
187     }
188
189     protected synchronized void gotoAbsPos(int absPos) throws IOException JavaDoc {
190         int i = 0;
191         int count = 0;
192
193         int size = bufferManager.getCount();
194         for(; i < size; i++) {
195             int length = bufferManager.getDataLength(i);
196             if((count + length) < absPos) {
197                 count += length;
198             }
199             else {
200                 break;
201             }
202         }
203
204         this.buf = bufferManager.getData(i);
205         pos = absPos - count;
206         totalRead = absPos;
207     }
208
209     public void reset() throws IOException JavaDoc {
210         gotoAbsPos(mark);
211     }
212
213     public boolean markSupported() {
214         return true;
215     }
216
217     public void debug(int len) throws IOException JavaDoc {
218         byte[] b = this.buf;
219         int p = pos;
220         int l = len;
221
222         //Sys.out.println("pos = " + totalRead + "; count = " + totalCount);
223

224         if((p + len) > b.length) {
225             len = b.length - p;
226         }
227
228         if(len == 0) {
229             b = bufferManager.getData(vpos);
230             len = l;
231         }
232
233         //Sys.out.println("len: " + len);
234

235         for(int i = 0; i < len; i++) {
236             int c = b[i + p] & 0xFF;
237             printHex(c);
238         }
239     }
240
241
242     public void debug() throws IOException JavaDoc {
243         int size = bufferManager.getCount();
244         for(int i = 0; i < size; i++) {
245             byte[] b = bufferManager.getData(i);
246             //Sys.out.println("############### " + i);
247
for(int j = 0; j < b.length; j++) {
248                 int c = b[j] & 0xFF;
249                 printHex(c);
250             }
251         }
252     }
253
254     public void debug(OutputStream JavaDoc out) throws IOException JavaDoc {
255         int size = bufferManager.getCount();
256         for(int i = 0; i < size; i++) {
257             byte[] b = bufferManager.getData(i);
258             for(int j = 0; j < b.length; j++) {
259                 int c = b[j] & 0xFF;
260                 out.write(c);
261             }
262         }
263     }
264 }
265
266
267
268
269
270
271
Popular Tags