KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > bytes > FileInputStream


1 /*
2   Copyright 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.bytes;
28
29 import java.io.RandomAccessFile JavaDoc;
30 import java.io.EOFException JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34   File stream.
35 */

36 public final class FileInputStream
37   implements IInputStream
38 {
39   // <class>
40
// <dynamic>
41
// <fields>
42
private RandomAccessFile JavaDoc file;
43   // </fields>
44

45   // <constructors>
46
public FileInputStream(
47     RandomAccessFile JavaDoc file
48     )
49   {
50     this.file = file;
51   }
52   // </constructors>
53

54   // <interface>
55
// <public>
56
// <IInputStream>
57
public long getLength(
58     )
59   {
60     try
61     {return file.length();}
62     catch(IOException JavaDoc e)
63     {throw new RuntimeException JavaDoc(e);}
64   }
65
66   public long getPosition(
67     )
68   {
69     try
70     {return file.getFilePointer();}
71     catch(IOException JavaDoc e)
72     {throw new RuntimeException JavaDoc(e);}
73   }
74
75   public int hashCode(
76     )
77   {return file.hashCode();}
78
79   public void read(
80     byte[] data
81     )
82     throws EOFException JavaDoc
83   {
84     try
85     {file.readFully(data);}
86     catch(EOFException JavaDoc e)
87     {throw e;}
88     catch(IOException JavaDoc e)
89     {throw new RuntimeException JavaDoc(e);}
90   }
91
92   public void read(
93     byte[] data,
94     int offset,
95     int length
96     )
97     throws EOFException JavaDoc
98   {
99     try
100     {file.readFully(data,offset,length);}
101     catch(EOFException JavaDoc e)
102     {throw e;}
103     catch(IOException JavaDoc e)
104     {throw new RuntimeException JavaDoc(e);}
105   }
106
107   public byte readByte(
108     )
109     throws EOFException JavaDoc
110   {
111     try
112     {return file.readByte();}
113     catch(EOFException JavaDoc e)
114     {throw e;}
115     catch(IOException JavaDoc e)
116     {throw new RuntimeException JavaDoc(e);}
117   }
118
119   public int readInt(
120     )
121     throws EOFException JavaDoc
122   {
123     try
124     {return file.readInt();}
125     catch(EOFException JavaDoc e)
126     {throw e;}
127     catch(IOException JavaDoc e)
128     {throw new RuntimeException JavaDoc(e);}
129   }
130
131   public String JavaDoc readLine(
132     )
133     throws EOFException JavaDoc
134   {
135     try
136     {return file.readLine();}
137     catch(EOFException JavaDoc e)
138     {throw e;}
139     catch(IOException JavaDoc e)
140     {throw new RuntimeException JavaDoc(e);}
141   }
142
143   public short readShort(
144     )
145     throws EOFException JavaDoc
146   {
147     try
148     {return file.readShort();}
149     catch(EOFException JavaDoc e)
150     {throw e;}
151     catch(IOException JavaDoc e)
152     {throw new RuntimeException JavaDoc(e);}
153   }
154
155   public String JavaDoc readString(
156     int length
157     )
158     throws EOFException JavaDoc
159   {
160     byte[] data = new byte[length];
161     try
162     {
163       file.readFully(data);
164
165       return new String JavaDoc(data,"ISO-8859-1");
166     }
167     catch(EOFException JavaDoc e)
168     {throw e;}
169     catch(Exception JavaDoc e)
170     {throw new RuntimeException JavaDoc(e);}
171   }
172
173   public int readUnsignedByte(
174     )
175     throws EOFException JavaDoc
176   {
177     try
178     {return file.readUnsignedByte();}
179     catch(EOFException JavaDoc e)
180     {throw e;}
181     catch(IOException JavaDoc e)
182     {throw new RuntimeException JavaDoc(e);}
183   }
184
185   public int readUnsignedShort(
186     )
187     throws EOFException JavaDoc
188   {
189     try
190     {return file.readUnsignedShort();}
191     catch(EOFException JavaDoc e)
192     {throw e;}
193     catch(IOException JavaDoc e)
194     {throw new RuntimeException JavaDoc(e);}
195   }
196
197   public void seek(
198     long offset
199     )
200   {
201     try
202     {file.seek(offset);}
203     catch(IOException JavaDoc e)
204     {throw new RuntimeException JavaDoc(e);}
205   }
206
207   public void setPosition(
208     long value
209     )
210   {
211     try
212     {file.seek(value);}
213     catch(IOException JavaDoc e)
214     {throw new RuntimeException JavaDoc(e);}
215   }
216
217   public void skip(
218     long offset
219     )
220   {
221     try
222     {file.seek(file.getFilePointer() + offset);}
223     catch(IOException JavaDoc e)
224     {throw new RuntimeException JavaDoc(e);}
225   }
226
227   public byte[] toByteArray(
228     )
229   {
230     byte[] data = null;
231     try
232     {
233       file.seek(0);
234       data = new byte[(int)file.length()];
235       file.readFully(data);
236     }
237     catch(IOException JavaDoc e)
238     {throw new RuntimeException JavaDoc(e);}
239
240     return data;
241   }
242   // </IInputStream>
243
// </public>
244
// </interface>
245
// </dynamic>
246
// </class>
247
}
Popular Tags