KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > InputStreamBufferManager


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.buffer;
33
34 import com.imagero.uio.io.IOutils;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38
39 /**
40  * @author Andrei Kouznetsov
41  * Date: 12.11.2003
42  * Time: 12:45:21
43  */

44 public class InputStreamBufferManager extends AbstractBufferManager {
45
46     InputStream JavaDoc in;
47
48     long sumLength;
49     int bufferCount;
50
51     /**
52      * create BufferManager for given InputStream (with standard length of 50k)
53      *
54      * @param in InputStream
55      */

56     public InputStreamBufferManager(InputStream JavaDoc in) {
57         this(defaultBufferSize, in);
58     }
59
60     /**
61      * create BufferManager for given InputStream
62      *
63      * @param bufferSize standard length of Buffer
64      * @param in InputStream
65      */

66     public InputStreamBufferManager(int bufferSize, InputStream JavaDoc in) {
67         this.bufferSize = bufferSize;
68         this.in = in;
69     }
70
71     /**
72      * get data (as byte array) from i'th Buffer
73      *
74      * @param i Buffer index
75      * @return byte array
76      * @throws IOException if i'th Buffer not exists and couldn't be read from InputStream
77      */

78     public byte[] getData(int i) throws IOException JavaDoc {
79         try {
80             return getDataImpl(i);
81         }
82         catch(ArrayIndexOutOfBoundsException JavaDoc ex) {
83             ex.printStackTrace();
84             throw new IOException JavaDoc();
85         }
86     }
87
88     /**
89      * read all data sequential
90      *
91      * @param i Buffer index
92      *
93      * @return byte array
94      *
95      * @throws IOException
96      */

97     protected byte[] getDataImpl(int i) throws IOException JavaDoc {
98         if(i >= bufferCount) {
99             fillBuffer(i);
100         }
101         Buffer buffer = accessManager.get(i);
102         if(buffer != null) {
103             return buffer.getData();
104         }
105         return empty;
106     }
107
108     /**
109      * read all Buffers from current index till <code>tillIndex</code> from InputStream
110      *
111      * @param tillIndex
112      */

113     protected void fillBuffer(int tillIndex) {
114         int count = tillIndex - bufferCount + 1;
115         int read = 0;
116         for(int i = 0; i < count; i++) {
117             byte[] data = new byte[bufferSize];
118             try {
119                 read = IOutils.readFully2(in, data);
120                 if(read < 0) {
121                     break;
122                 }
123                 else {
124                     byte[] data0 = data;
125                     if(read != data.length) {
126                         data0 = new byte[read];
127                         System.arraycopy(data, 0, data0, 0, read);
128                     }
129                     Buffer ds0 = new ByteBuffer(data0);
130                     accessManager.add(ds0);
131                     bufferCount++;
132                     sumLength += read;
133                 }
134             }
135             catch(IOException JavaDoc ex) {
136                 ex.printStackTrace();
137                 break;
138             }
139         }
140     }
141
142     /**
143      * get length of i'th Buffer
144      *
145      * @param i Buffer index
146      *
147      * @return int
148      *
149      * @throws ArrayIndexOutOfBoundsException if i'th Buffer not exists (e.g. wasn't yet read)
150      */

151     public int getDataLength(int i) {
152         return accessManager.getBufferLength(i);
153     }
154
155     /**
156      * get index of Buffer which contains <code>pos</code>
157      *
158      * @param pos
159      *
160      * @return index of Buffer or -1
161      */

162     public int getIndex(long pos) {
163         if(pos < 0) {
164             return -1;
165         }
166
167         return (int) (pos / bufferSize);
168     }
169
170     /**
171      * get length of data of all already read Buffer together (may change)
172      *
173      */

174     public long getLength() {
175         return sumLength;
176     }
177
178     /**
179      * get start of i'th Buffer in byte<br>
180      * I assume here that length of each Buffer (except last one) equals to <code>dsLength</code>
181      *
182      * @param i
183      *
184      */

185     public long getDataStart(int i) {
186         return bufferSize * i;
187     }
188
189     /**
190      * closes underlined InputStream and drops all Buffers
191      */

192     public void close() {
193         IOutils.closeStream(in);
194         clear();
195     }
196 }
197
Popular Tags