KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > io > DataBufferInputStream


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 package com.nightlabs.io;
32
33 import java.io.*;
34 import java.io.IOException JavaDoc;
35
36
37 /**
38  * An InputStream that writes to a {@link com.nightlabs.io.DataBuffer}.
39  * @author Marco Schulze
40  */

41 public class DataBufferInputStream extends InputStream
42 {
43   DataBuffer dataBuffer;
44
45   protected DataBufferInputStream(DataBuffer _dataBuffer)
46   {
47     this.dataBuffer = _dataBuffer;
48   }
49
50 // public DataBufferInputStream(IDataBuffer dataBuffer, boolean seekToBeginning)
51
// throws IOException
52
// {
53
// this.dataBuffer = dataBuffer;
54
//
55
// if (seekToBeginning)
56
// this.dataBuffer.seekToBeginning();
57
// }
58

59   protected long markedReadPos = 0;
60   protected long dataReadPos = 0;
61
62   /**
63    * Is initialized when the dataBuffer switches to fileMode after this dataBuffer
64    * has already been created. This happens when the next read occurs.
65    */

66   protected BufferedInputStream in = null;
67
68
69   public int available()
70   throws IOException JavaDoc
71   {
72     if (in != null)
73       return in.available();
74
75     long avl = dataBuffer.length - dataReadPos;
76
77     if (avl <= 0)
78       return 0;
79
80     if (avl > Integer.MAX_VALUE)
81       return Integer.MAX_VALUE;
82
83     return (int)avl;
84   }
85
86   protected boolean closed = false;
87
88   public void close()
89   throws IOException JavaDoc
90   {
91     if (in != null)
92       in.close();
93
94     closed = true;
95   }
96
97   protected int markReadLimit = 0;
98   public void mark(int readlimit)
99   {
100     if (in != null)
101       in.mark(readlimit);
102
103     markReadLimit = readlimit;
104     markedReadPos = dataReadPos;
105   }
106
107   public boolean markSupported()
108   {
109     return true;
110   }
111
112   protected void checkMode()
113   throws IOException JavaDoc
114   {
115     if (in == null && dataBuffer.mode == DataBuffer.MODE_FILE) {
116       in = new BufferedInputStream(dataBuffer.createFileInputStream());
117       if (markedReadPos > 0) {
118         in.skip(markedReadPos);
119         in.mark(markReadLimit);
120       }
121       in.skip(dataReadPos - markedReadPos);
122     }
123   }
124
125   public int read()
126   throws IOException JavaDoc
127   {
128     checkMode();
129
130     if (closed)
131       throw new IOException JavaDoc("InputStream has already been closed!");
132
133     if (in != null) {
134       int i = in.read();
135       if (i >= 0)
136         dataReadPos++;
137       return i;
138     }
139
140     if (dataReadPos > Integer.MAX_VALUE - 10)
141       throw new IllegalStateException JavaDoc("dataReadPos is too big! Should have switched to file mode earlier!");
142
143     if (dataReadPos < dataBuffer.length)
144       return ((int)dataBuffer.data[(int)dataReadPos++]) & 0xff;
145     else
146       return -1;
147   }
148
149   public int read(byte[] b, int off, int len)
150   throws IOException JavaDoc
151   {
152     checkMode();
153
154     if (closed)
155       throw new IOException JavaDoc("InputStream has already been closed!");
156
157     int dr;
158
159     if (in != null)
160       dr = in.read(b, off, len);
161     else {
162       if (dataReadPos + len > dataBuffer.length)
163         len = (int)(dataBuffer.length - dataReadPos);
164
165       if (dataReadPos > Integer.MAX_VALUE - 10)
166         throw new IllegalStateException JavaDoc("dataReadPos is too big! Should have switched to file mode earlier!");
167
168       if (len > 0) {
169         System.arraycopy(dataBuffer.data, (int)dataReadPos, b, off, len);
170         dr = len;
171       }
172       else
173         dr = -1;
174     }
175
176     if (dr > 0)
177       dataReadPos += dr;
178
179     return dr;
180   }
181
182   public int read(byte[] b)
183   throws IOException JavaDoc
184   {
185     return read(b, 0, b.length);
186   }
187
188
189
190
191
192
193
194 // public int read()
195
// throws IOException
196
// {
197
// return dataBuffer.read();
198
// }
199
//
200
// public int read(byte[] b)
201
// throws IOException
202
// {
203
// return dataBuffer.read(b);
204
// }
205
//
206
// public int read(byte[] b, int off, int len)
207
// throws IOException
208
// {
209
// return dataBuffer.read(b, off, len);
210
// }
211

212
213
214
215   public void reset()
216   throws IOException JavaDoc
217   {
218     checkMode();
219
220     if (in != null)
221       in.reset();
222
223     dataReadPos = markedReadPos;
224   }
225
226   public long skip(long n)
227   throws IOException JavaDoc
228   {
229     checkMode();
230     if (in != null)
231       n = in.skip(n);
232     else {
233       long max = dataBuffer.length - dataReadPos;
234       if (n > max) n = max;
235       dataReadPos += n;
236     }
237     return n;
238   }
239
240
241 }
Popular Tags