KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > VFSInputStream


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <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, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import com.sslexplorer.vfs.webdav.DAVException;
24
25
26 /**
27  * <p>A specialized {@link InputStream} to read from {@link VFSResource}s.</p>
28  *
29  * <p>This specialized {@link InputStream} never throws {@link IOException}s,
30  * but rather relies on the unchecked {@link DAVException} to notify the
31  * framework of the correct DAV errors.</p>
32  *
33  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
34  */

35 public class VFSInputStream extends InputStream JavaDoc {
36
37     /** <p>The {@link InputStream} of the source {@link File}. </p> */
38     private InputStream JavaDoc input = null;
39     /** <p>The {@link VFSResource} associated with this instance. </p> */
40     private VFSResource resource = null;
41
42     /**
43      * <p>Create a new {@link VFSInputStream} instance.</p>
44      */

45     protected VFSInputStream(VFSResource resource) {
46         if (resource == null) throw new NullPointerException JavaDoc();
47         try {
48
49             if(resource instanceof FileObjectVFSResource) {
50                 this.input = ((FileObjectVFSResource)resource).getFile().getContent().getInputStream();
51             }
52             else {
53                 throw new IOException JavaDoc("DAV resource is not a true file.");
54             }
55         } catch (IOException JavaDoc e) {
56             String JavaDoc message = "Unable to read from resource";
57             throw new DAVException (403, message, e, resource);
58         }
59     }
60
61     /**
62      * <p>Read data from this {@link InputStream}.</p>
63      */

64     public int read() {
65         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
66         try {
67             return input.read();
68         } catch (IOException JavaDoc e) {
69             throw new DAVException(403, "Can't read data", e, this.resource);
70         }
71     }
72
73     /**
74      * <p>Read data from this {@link InputStream}.</p>
75      */

76     public int read(byte b[]) {
77         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
78         try {
79             return input.read(b);
80         } catch (IOException JavaDoc e) {
81             throw new DAVException(403, "Can't read data", e, this.resource);
82         }
83     }
84
85     /**
86      * <p>Read data from this {@link InputStream}.</p>
87      */

88     public int read(byte b[], int off, int len) {
89         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
90         try {
91             return input.read(b, off, len);
92         } catch (IOException JavaDoc e) {
93             throw new DAVException(403, "Can't read data", e, this.resource);
94         }
95     }
96
97     /**
98      * <p>Skip a specified amount of data reading from this
99      * {@link InputStream}.</p>
100      */

101     public long skip(long n) {
102         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
103         try {
104             return input.skip(n);
105         } catch (IOException JavaDoc e) {
106             throw new DAVException(403, "Can't skip over", e, this.resource);
107         }
108     }
109
110     /**
111      * <p>Return the number of bytes that can be read or skipped from this
112      * {@link InputStream} without blocking.</p>
113      */

114     public int available() {
115         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
116         try {
117             return input.available();
118         } catch (IOException JavaDoc e) {
119             throw new DAVException(403, "Can't skip over", e, this.resource);
120         }
121     }
122
123     /**
124      * <p>Return the number of bytes that can be read or skipped from this
125      * {@link InputStream} without blocking.</p>
126      */

127     public void close() {
128         if (this.input == null) return;
129         try {
130             this.input.close();
131         } catch (IOException JavaDoc e) {
132             throw new DAVException(403, "Can't close", e, this.resource);
133         } finally {
134             this.input = null;
135         }
136     }
137     
138     /**
139      * <p>Marks the current position in this {@link InputStream}.</p>
140      */

141     public void mark(int readlimit) {
142         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
143         this.input.mark(readlimit);
144     }
145     
146     /**
147      * <p>Repositions this stream to the position at the time the
148      * {@link #mark(int)} method was last called on this
149      * {@link InputStream}.</p>
150      */

151     public void reset() {
152         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
153         try {
154             input.reset();
155         } catch (IOException JavaDoc e) {
156             throw new DAVException(403, "Can't reset", e, this.resource);
157         }
158     }
159     
160     /**
161      * <p>Tests if this {@link InputStream} supports the {@link #mark(int)}
162      * and {@link #reset()} methods.</p>
163      */

164     public boolean markSupported() {
165         if (this.input == null) throw new IllegalStateException JavaDoc("Closed");
166         return this.input.markSupported();
167     }
168 }
169
Popular Tags