KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > remoteio > RemoteInputStreamAdapter


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.remoteio;
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10
11 //import org.apache.commons.logging.Log;
12
//import org.apache.commons.logging.LogFactory;
13

14 /**
15  *
16  * @author Carlos Arévalo
17  */

18 public class RemoteInputStreamAdapter extends InputStream JavaDoc
19 {
20     //private static Log LOG = LogFactory.getLog(RemoteInputStreamAdapter.class);
21

22     private RemoteInputStream remoteInputStream = null;
23     private BufferedInputStream JavaDoc bufferedInputStream = null;
24     private RawInputStream rawInputStream = null;
25     private OctetSequenceHolder bufferHolder = null;
26
27     /**
28      * Class constructor
29      * @param remoteStream the remote input stream that this adapter takes
30      */

31     public RemoteInputStreamAdapter(RemoteInputStream remoteStream)
32     {
33         this.remoteInputStream = remoteStream;
34         this.rawInputStream = new RawInputStream();
35         this.bufferedInputStream = new BufferedInputStream JavaDoc(rawInputStream);
36         bufferHolder = new OctetSequenceHolder();
37     }
38
39     /**
40      * @see java.io.InputStream#read()
41      */

42     public int read() throws IOException JavaDoc
43     {
44         return this.bufferedInputStream.read();
45     }
46
47     /**
48      * @see java.io.InputStream#read(byte[], int, int)
49      */

50     public int read(byte[] b, int off, int len) throws IOException JavaDoc
51     {
52         return this.bufferedInputStream.read(b, off, len);
53     }
54
55     /**
56      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#close()
57      */

58     public void close() throws IOException JavaDoc
59     {
60         this.bufferedInputStream.close();
61     }
62
63     /**
64      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#available()
65      */

66     public int available() throws IOException JavaDoc
67     {
68         return this.bufferedInputStream.available();
69     }
70
71     /**
72      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#mark(int)
73      */

74     public void mark(int readlimit)
75     {
76         this.bufferedInputStream.mark(readlimit);
77     }
78
79     /**
80      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#markSupported()
81      */

82     public boolean markSupported()
83     {
84         return this.bufferedInputStream.markSupported();
85     }
86
87     /**
88      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#reset()
89      */

90     public void reset() throws IOException JavaDoc
91     {
92         this.bufferedInputStream.reset();
93     }
94
95     /**
96      * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#skip(long)
97      */

98     public long skip(long n) throws IOException JavaDoc
99     {
100         return this.bufferedInputStream.skip(n);
101     }
102
103     /**
104      * This class is used to actually read the data from the local file
105      * @author Carlos Arévalo
106      */

107     class RawInputStream extends InputStream JavaDoc
108     {
109         /**
110          * @see java.io.InputStream#read()
111          */

112         public int read() throws IOException JavaDoc
113         {
114             byte[] b = {0};
115             if (this.read(b, 0, 1) == -1)
116             {
117                 return -1;
118             }
119             return b[0];
120         }
121
122         /**
123          * @see java.io.InputStream#read(byte[], int, int)
124          */

125         public int read(byte[] b, int off, int len) throws IOException JavaDoc
126         {
127             try
128             {
129                 int bytesRead = remoteInputStream.read(bufferHolder, len);
130                 System.arraycopy(bufferHolder.value, 0, b, off, len);
131                 return bytesRead;
132             }
133             catch (RemoteIOException e)
134             {
135                 e.printStackTrace();
136                 throw new IOException JavaDoc(e.getMessage());
137             }
138         }
139
140         /**
141          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#close()
142          */

143         public void close() throws IOException JavaDoc
144         {
145             try
146             {
147                 remoteInputStream.close();
148             }
149             catch (RemoteIOException e)
150             {
151                 throw new IOException JavaDoc(e.getMessage());
152             }
153         }
154
155         /**
156          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#available()
157          */

158         public int available() throws IOException JavaDoc
159         {
160             try
161             {
162                 return remoteInputStream.available();
163             }
164             catch (RemoteIOException e)
165             {
166                 throw new IOException JavaDoc(e.getMessage());
167             }
168         }
169
170         /**
171          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#mark(int)
172          */

173         public void mark(int readlimit)
174         {
175             remoteInputStream.mark(readlimit);
176         }
177
178         /**
179          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#markSupported()
180          */

181         public boolean markSupported()
182         {
183             return remoteInputStream.markSupported();
184         }
185
186         /**
187          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#reset()
188          */

189         public void reset() throws IOException JavaDoc
190         {
191             try
192             {
193                 remoteInputStream.reset();
194             }
195             catch (RemoteIOException e)
196             {
197                 throw new IOException JavaDoc(e.getMessage());
198             }
199         }
200
201         /**
202          * @see ve.luz.ica.remoteio.RemoteInputStreamOperations#skip(long)
203          */

204         public long skip(long n) throws IOException JavaDoc
205         {
206             try
207             {
208                 return remoteInputStream.skip(n);
209             }
210             catch (RemoteIOException e)
211             {
212                 throw new IOException JavaDoc(e.getMessage());
213             }
214         }
215     }
216
217 }
218
Popular Tags