KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > smb > SmbFileRandomAccessContent


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.smb;
17
18 import jcifs.smb.SmbException;
19 import jcifs.smb.SmbFile;
20 import jcifs.smb.SmbRandomAccessFile;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
23 import org.apache.commons.vfs.util.RandomAccessMode;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29
30 /**
31  * RandomAccess for smb files
32  *
33  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
34  */

35 class SmbFileRandomAccessContent extends AbstractRandomAccessContent
36 {
37     private final SmbRandomAccessFile raf;
38     private final InputStream JavaDoc rafis;
39
40     SmbFileRandomAccessContent(final SmbFile smbFile, final RandomAccessMode mode) throws FileSystemException
41     {
42         super(mode);
43
44         StringBuffer JavaDoc modes = new StringBuffer JavaDoc(2);
45         if (mode.requestRead())
46         {
47             modes.append('r');
48         }
49         if (mode.requestWrite())
50         {
51             modes.append('w');
52         }
53
54         try
55         {
56             raf = new SmbRandomAccessFile(smbFile, modes.toString());
57             rafis = new InputStream JavaDoc()
58             {
59                 public int read() throws IOException JavaDoc
60                 {
61                     return raf.readByte();
62                 }
63
64                 public long skip(long n) throws IOException JavaDoc
65                 {
66                     raf.seek(raf.getFilePointer() + n);
67                     return n;
68                 }
69
70                 public void close() throws IOException JavaDoc
71                 {
72                     raf.close();
73                 }
74
75                 public int read(byte b[]) throws IOException JavaDoc
76                 {
77                     return raf.read(b);
78                 }
79
80                 public int read(byte b[], int off, int len) throws IOException JavaDoc
81                 {
82                     return raf.read(b, off, len);
83                 }
84             };
85         }
86         catch (MalformedURLException JavaDoc e)
87         {
88             throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
89         }
90         catch (SmbException e)
91         {
92             throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
93         }
94         catch (UnknownHostException JavaDoc e)
95         {
96             throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
97         }
98     }
99
100     public long getFilePointer() throws IOException JavaDoc
101     {
102         return raf.getFilePointer();
103     }
104
105     public void seek(long pos) throws IOException JavaDoc
106     {
107         raf.seek(pos);
108     }
109
110     public long length() throws IOException JavaDoc
111     {
112         return raf.length();
113     }
114
115     public void close() throws IOException JavaDoc
116     {
117         raf.close();
118     }
119
120     public byte readByte() throws IOException JavaDoc
121     {
122         return raf.readByte();
123     }
124
125     public char readChar() throws IOException JavaDoc
126     {
127         return raf.readChar();
128     }
129
130     public double readDouble() throws IOException JavaDoc
131     {
132         return raf.readDouble();
133     }
134
135     public float readFloat() throws IOException JavaDoc
136     {
137         return raf.readFloat();
138     }
139
140     public int readInt() throws IOException JavaDoc
141     {
142         return raf.readInt();
143     }
144
145     public int readUnsignedByte() throws IOException JavaDoc
146     {
147         return raf.readUnsignedByte();
148     }
149
150     public int readUnsignedShort() throws IOException JavaDoc
151     {
152         return raf.readUnsignedShort();
153     }
154
155     public long readLong() throws IOException JavaDoc
156     {
157         return raf.readLong();
158     }
159
160     public short readShort() throws IOException JavaDoc
161     {
162         return raf.readShort();
163     }
164
165     public boolean readBoolean() throws IOException JavaDoc
166     {
167         return raf.readBoolean();
168     }
169
170     public int skipBytes(int n) throws IOException JavaDoc
171     {
172         return raf.skipBytes(n);
173     }
174
175     public void readFully(byte b[]) throws IOException JavaDoc
176     {
177         raf.readFully(b);
178     }
179
180     public void readFully(byte b[], int off, int len) throws IOException JavaDoc
181     {
182         raf.readFully(b, off, len);
183     }
184
185     public String JavaDoc readUTF() throws IOException JavaDoc
186     {
187         return raf.readUTF();
188     }
189
190     public void writeDouble(double v) throws IOException JavaDoc
191     {
192         raf.writeDouble(v);
193     }
194
195     public void writeFloat(float v) throws IOException JavaDoc
196     {
197         raf.writeFloat(v);
198     }
199
200     public void write(int b) throws IOException JavaDoc
201     {
202         raf.write(b);
203     }
204
205     public void writeByte(int v) throws IOException JavaDoc
206     {
207         raf.writeByte(v);
208     }
209
210     public void writeChar(int v) throws IOException JavaDoc
211     {
212         raf.writeChar(v);
213     }
214
215     public void writeInt(int v) throws IOException JavaDoc
216     {
217         raf.writeInt(v);
218     }
219
220     public void writeShort(int v) throws IOException JavaDoc
221     {
222         raf.writeShort(v);
223     }
224
225     public void writeLong(long v) throws IOException JavaDoc
226     {
227         raf.writeLong(v);
228     }
229
230     public void writeBoolean(boolean v) throws IOException JavaDoc
231     {
232         raf.writeBoolean(v);
233     }
234
235     public void write(byte b[]) throws IOException JavaDoc
236     {
237         raf.write(b);
238     }
239
240     public void write(byte b[], int off, int len) throws IOException JavaDoc
241     {
242         raf.write(b, off, len);
243     }
244
245     public void writeBytes(String JavaDoc s) throws IOException JavaDoc
246     {
247         raf.writeBytes(s);
248     }
249
250     public void writeChars(String JavaDoc s) throws IOException JavaDoc
251     {
252         raf.writeChars(s);
253     }
254
255     public void writeUTF(String JavaDoc str) throws IOException JavaDoc
256     {
257         raf.writeUTF(str);
258     }
259
260     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
261     {
262         return rafis;
263     }
264 }
265
Popular Tags