KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > FileRandomAccessStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.RandomAccessFile JavaDoc;
38
39 /**
40  * Reads from a file in a random-access fashion.
41  */

42 public class FileRandomAccessStream extends RandomAccessStream {
43   private RandomAccessFile JavaDoc _file;
44   private OutputStream JavaDoc _os;
45   private InputStream JavaDoc _is;
46
47   public FileRandomAccessStream(RandomAccessFile JavaDoc file)
48   {
49     _file = file;
50   }
51
52   public RandomAccessFile JavaDoc getRandomAccessFile()
53   {
54     return _file;
55   }
56   
57   /**
58    * Returns the length.
59    */

60   public long getLength()
61     throws IOException JavaDoc
62   {
63     return _file.length();
64   }
65   
66   /**
67    * Reads a block starting from the current file pointer.
68    */

69   public int read(byte []buffer, int offset, int length)
70     throws IOException JavaDoc
71   {
72     return _file.read(buffer, offset, length);
73   }
74
75   /**
76    * Reads a block from a given location.
77    */

78   public int read(long fileOffset, byte []buffer, int offset, int length)
79     throws IOException JavaDoc
80   {
81     _file.seek(fileOffset);
82     
83     return _file.read(buffer, offset, length);
84   }
85
86   /**
87    * Writes a block starting from the current file pointer.
88    */

89   public void write(byte []buffer, int offset, int length)
90     throws IOException JavaDoc
91   {
92     _file.write(buffer, offset, length);
93   }
94
95   /**
96    * Writes a block from a given location.
97    */

98   public void write(long fileOffset, byte []buffer, int offset, int length)
99     throws IOException JavaDoc
100   {
101     _file.seek(fileOffset);
102     
103     _file.write(buffer, offset, length);
104   }
105
106   /**
107    * Seeks to the given position in the file.
108    */

109   public boolean seek(long position)
110   {
111     try {
112       _file.seek(position);
113
114       return true;
115     } catch (IOException JavaDoc e) {
116       return false;
117     }
118   }
119
120   /**
121    * Returns an OutputStream for this stream.
122    */

123   public OutputStream JavaDoc getOutputStream()
124     throws IOException JavaDoc
125   {
126     if (_os == null)
127       _os = new FileOutputStream JavaDoc(_file.getFD());
128
129     return _os;
130   }
131
132   /**
133    * Returns an InputStream for this stream.
134    */

135   public InputStream JavaDoc getInputStream()
136     throws IOException JavaDoc
137   {
138     if (_is == null)
139       _is = new FileInputStream JavaDoc(_file.getFD());
140
141     return _is;
142   }
143
144   /**
145    * Read a byte from the file, advancing the pointer.
146    */

147   public int read()
148     throws IOException JavaDoc
149   {
150     return _file.read();
151   }
152
153   /**
154    * Write a byte to the file, advancing the pointer.
155    */

156   public void write(int b)
157     throws IOException JavaDoc
158   {
159     _file.write(b);
160   }
161
162   /**
163    * Returns the current position of the file pointer.
164    */

165   public long getFilePointer()
166     throws IOException JavaDoc
167   {
168     return _file.getFilePointer();
169   }
170
171   /**
172    * Closes the stream.
173    */

174   public void close() throws IOException JavaDoc
175   {
176     RandomAccessFile JavaDoc file = _file;
177     _file = null;
178
179     if (file != null)
180       file.close();
181   }
182 }
183
Popular Tags