KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32
33 /**
34  * Stream using with JNI.
35  */

36 public class JniFileStream extends StreamImpl {
37   private int _fd;
38   private long _pos;
39
40   private boolean _canRead;
41   private boolean _canWrite;
42
43   /**
44    * Create a new JniStream based on the java.io.* stream.
45    */

46   public JniFileStream(int fd, boolean canRead, boolean canWrite)
47   {
48     init(fd, canRead, canWrite);
49   }
50
51   void init(int fd, boolean canRead, boolean canWrite)
52   {
53     _fd = fd;
54     _pos = 0;
55
56     _canRead = canRead;
57     _canWrite = canWrite;
58   }
59
60   public boolean canRead()
61   {
62     return _canRead && _fd >= 0;
63   }
64
65   public boolean hasSkip()
66   {
67     return _fd >= 0;
68   }
69
70   public long skip(long length)
71     throws IOException JavaDoc
72   {
73     long pos = nativeSkip(_fd, length);
74
75     if (pos < 0)
76       return -1;
77     else {
78       _pos = pos;
79       return length;
80     }
81   }
82
83   /**
84    * Reads data from the file.
85    */

86   public int read(byte []buf, int offset, int length)
87     throws IOException JavaDoc
88   {
89     if (buf == null)
90       throw new NullPointerException JavaDoc();
91     else if (offset < 0 || buf.length < offset + length)
92       throw new ArrayIndexOutOfBoundsException JavaDoc();
93
94     int result = nativeRead(_fd, buf, offset, length);
95
96     if (result > 0)
97       _pos += result;
98
99     return result;
100   }
101
102   // XXX: needs update
103
public int getAvailable() throws IOException JavaDoc
104   {
105     if (_fd < 0) {
106       return -1;
107     }
108     else if (getPath() instanceof FilesystemPath) {
109       long length = getPath().getLength();
110       
111       return (int) (length - _pos);
112     }
113     else
114       return nativeAvailable(_fd);
115   }
116
117   /**
118    * Returns true if this is a writeable stream.
119    */

120   public boolean canWrite()
121   {
122     return _canWrite && _fd >= 0;
123   }
124
125   /**
126    * Writes data to the file.
127    */

128   public void write(byte []buf, int offset, int length, boolean isEnd)
129     throws IOException JavaDoc
130   {
131     if (buf == null)
132       throw new NullPointerException JavaDoc();
133     else if (offset < 0 || buf.length < offset + length)
134       throw new ArrayIndexOutOfBoundsException JavaDoc();
135
136     nativeWrite(_fd, buf, offset, length);
137   }
138
139   public void seekStart(long offset)
140     throws IOException JavaDoc
141   {
142     nativeSeekStart(_fd, offset);
143   }
144
145   public void seekEnd(long offset)
146     throws IOException JavaDoc
147   {
148     nativeSeekEnd(_fd, offset);
149   }
150
151   public void flush()
152     throws IOException JavaDoc
153   {
154   }
155
156   public void flushToDisk()
157     throws IOException JavaDoc
158   {
159     nativeFlushToDisk(_fd);
160   }
161
162   public void close()
163     throws IOException JavaDoc
164   {
165     int fd;
166
167     synchronized (this) {
168       fd = _fd;
169       _fd = -1;
170     }
171     
172     nativeClose(fd);
173   }
174
175   protected void finalize()
176     throws IOException JavaDoc
177   {
178     close();
179   }
180
181   /**
182    * Native interface to read bytes from the input.
183    */

184   native int nativeRead(int fd, byte []buf, int offset, int length)
185     throws IOException JavaDoc;
186
187   /**
188    * Native interface to read bytes from the input.
189    */

190   native int nativeAvailable(int fd)
191     throws IOException JavaDoc;
192
193   /**
194    * Native interface to write bytes to the file
195    */

196   native int nativeWrite(int fd, byte []buf, int offset, int length)
197     throws IOException JavaDoc;
198
199   /**
200    * Native interface to skip bytes from the input.
201    */

202   native long nativeSkip(int fd, long skip)
203     throws IOException JavaDoc;
204
205   /**
206    * Native interface to force data to the disk
207    */

208   native int nativeFlushToDisk(int fd)
209     throws IOException JavaDoc;
210
211   /**
212    * Native interface to read bytes from the input.
213    */

214   native int nativeClose(int fd)
215     throws IOException JavaDoc;
216
217   /**
218    * Native interface to seek from the beginning
219    */

220   native int nativeSeekStart(int fd, long offset)
221     throws IOException JavaDoc;
222
223   /**
224    * Native interface to seek from the beginning
225    */

226   native int nativeSeekEnd(int fd, long offset)
227     throws IOException JavaDoc;
228
229   /**
230    * Returns the debug name for the stream.
231    */

232   public String JavaDoc toString()
233   {
234     return "JniFileStream[" + getPath().getNativePath() + "]";
235   }
236
237   static {
238     try {
239       System.loadLibrary("resin");
240     } catch (Throwable JavaDoc e) {
241       System.err.println("Can't open Resin JNI library: " + e);
242     }
243   }
244 }
245
246
Popular Tags