KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > smb > SmbFileInputStream


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs.smb;
20
21 import java.net.URL JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import com.knowgate.debug.*;
28
29 /**
30  * This InputStream can read bytes from a file on an SMB file server. Offsets are 64 bits.
31  */

32
33 public class SmbFileInputStream extends InputStream JavaDoc {
34
35     private SmbFile file;
36     private long fp;
37     private int readSize, openFlags;
38     private byte[] tmp = new byte[1];
39
40 /**
41  * Creates an {@link java.io.InputStream} for reading bytes from a file on
42  * an SMB server addressed by the <code>url</code> parameter. See {@link
43  * jcifs.smb.SmbFile} for a detailed description and examples of the smb
44  * URL syntax.
45  *
46  * @param url An smb URL string representing the file to read from
47  * @return A new <code>InputStream</code> for the specified <code>SmbFile</code>
48  */

49
50     public SmbFileInputStream( String JavaDoc url ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
51         this( new SmbFile( url ));
52     }
53
54 /**
55  * Creates an {@link java.io.InputStream} for reading bytes from a file on
56  * an SMB server represented by the {@link jcifs.smb.SmbFile} parameter. See
57  * {@link jcifs.smb.SmbFile} for a detailed description and examples of
58  * the smb URL syntax.
59  *
60  * @param url An smb URL string representing the file to write to
61  * @return A new <code>InputStream</code> for the specified <code>SmbFile</code>
62  */

63
64     public SmbFileInputStream( SmbFile file ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
65         this( file, SmbFile.O_RDONLY );
66     }
67
68     SmbFileInputStream( SmbFile file, int openFlags ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
69         this.file = file;
70         this.openFlags = openFlags;
71         file.open( openFlags, SmbFile.ATTR_NORMAL, 0 );
72         readSize = Math.min( file.tree.session.transport.rcv_buf_size - 70,
73                             file.tree.session.transport.server.maxBufferSize - 70 );
74     }
75
76 /**
77  * Closes this input stream and releases any system resources associated with the stream.
78  *
79  * @throws IOException if a network error occurs
80  */

81
82     public void close() throws IOException JavaDoc {
83         file.close();
84     }
85
86 /**
87  * Reads a byte of data from this input stream.
88  *
89  * @throws IOException if a network error occurs
90  */

91
92     public int read() throws IOException JavaDoc {
93         // need oplocks to cache otherwise use BufferedInputStream
94
if( read( tmp, 0, 1 ) == -1 ) {
95             return -1;
96         }
97         return tmp[0] & 0xFF;
98     }
99
100 /**
101  * Reads up to b.length bytes of data from this input stream into an array of bytes.
102  *
103  * @throws IOException if a network error occurs
104  */

105
106     public int read( byte[] b ) throws IOException JavaDoc {
107         return read( b, 0, b.length );
108     }
109
110 /**
111  * Reads up to len bytes of data from this input stream into an array of bytes.
112  *
113  * @throws IOException if a network error occurs
114  */

115
116     public int read( byte[] b, int off, int len ) throws IOException JavaDoc {
117         if( len <= 0 ) {
118             return 0;
119         }
120         long start = fp;
121
122         // ensure file is open
123
file.open( openFlags, SmbFile.ATTR_NORMAL, 0 );
124
125         /*
126          * Read AndX Request / Response
127          */

128
129         if( DebugFile.trace )
130             DebugFile.writeln( "read: fid=" + file.fid + ",off=" + off + ",len=" + len );
131
132         SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off );
133
134         if( file.type == SmbFile.TYPE_NAMED_PIPE ) {
135             response.responseTimeout = 0;
136         }
137
138         int r, n;
139         do {
140             r = len > readSize ? readSize : len;
141
142             if( DebugFile.trace )
143                 DebugFile.writeln( "read: len=" + len + ",r=" + r + ",fp=" + fp );
144
145             try {
146                 file.send( new SmbComReadAndX( file.fid, fp, r, null ), response );
147             } catch( SmbException se ) {
148                 if( file.type == SmbFile.TYPE_NAMED_PIPE &&
149                         se.getNtStatus() == NtStatus.NT_STATUS_PIPE_BROKEN ) {
150                     return -1;
151                 }
152                 throw se;
153             }
154             if(( n = response.dataLength ) <= 0 ) {
155                 return (int)((fp - start) > 0L ? fp - start : -1);
156             }
157             fp += n;
158             len -= n;
159             response.off += n;
160         } while( len > 0 && n == r );
161
162         return (int)(fp - start);
163     }
164     public int available() throws IOException JavaDoc {
165         SmbNamedPipe pipe;
166         TransPeekNamedPipe req;
167         TransPeekNamedPipeResponse resp;
168
169         if( file.type != SmbFile.TYPE_NAMED_PIPE ) {
170             return 0;
171         }
172
173         pipe = (SmbNamedPipe)file;
174         file.open(( pipe.pipeType & 0xFF0000 ) | SmbFile.O_EXCL, SmbFile.ATTR_NORMAL, 0 );
175
176         req = new TransPeekNamedPipe( file.unc, file.fid );
177         resp = new TransPeekNamedPipeResponse( pipe );
178
179         pipe.sendTransaction( req, resp );
180         if( resp.status == TransPeekNamedPipeResponse.STATUS_DISCONNECTED ||
181                 resp.status == TransPeekNamedPipeResponse.STATUS_SERVER_END_CLOSED ) {
182             file.opened = false;
183             return 0;
184         }
185         return resp.available;
186     }
187 /**
188  * Skip n bytes of data on this stream. This operation will not result
189  * in any IO with the server. Unlink <tt>InputStream</tt> value less than
190  * the one provided will not be returned if it exceeds the end of the file
191  * (if this is a problem let us know).
192  */

193     public long skip( long n ) throws IOException JavaDoc {
194         if (n > 0) {
195             fp += n;
196             return n;
197         }
198         return 0;
199     }
200 }
201
202
Popular Tags