KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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 /**
28  * This InputStream can read bytes from a file on an SMB file server. Offsets are 64 bits.
29  */

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

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

60
61     public SmbFileInputStream( SmbFile file ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
62         this( file, SmbFile.O_RDONLY );
63     }
64
65     SmbFileInputStream( SmbFile file, int openFlags ) throws SmbException, MalformedURLException JavaDoc, UnknownHostException JavaDoc {
66         this.file = file;
67         this.openFlags = openFlags & 0xFFFF;
68         this.access = (openFlags >>> 16) & 0xFFFF;
69         if (file.type != SmbFile.TYPE_NAMED_PIPE) {
70             file.open( openFlags, access, SmbFile.ATTR_NORMAL, 0 );
71             this.openFlags &= ~(SmbFile.O_CREAT | SmbFile.O_TRUNC);
72         } else {
73             file.connect0();
74         }
75         readSize = Math.min( file.tree.session.transport.rcv_buf_size - 70,
76                             file.tree.session.transport.server.maxBufferSize - 70 );
77     }
78
79 /**
80  * Closes this input stream and releases any system resources associated with the stream.
81  *
82  * @throws IOException if a network error occurs
83  */

84
85     public void close() throws IOException JavaDoc {
86         file.close();
87         tmp = null;
88     }
89
90 /**
91  * Reads a byte of data from this input stream.
92  *
93  * @throws IOException if a network error occurs
94  */

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

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

119
120     public int read( byte[] b, int off, int len ) throws IOException JavaDoc {
121         return readDirect(b, off, len);
122     }
123     public int readDirect( byte[] b, int off, int len ) throws IOException JavaDoc {
124         if( len <= 0 ) {
125             return 0;
126         }
127         long start = fp;
128
129         if( tmp == null ) {
130             throw new IOException JavaDoc( "Bad file descriptor" );
131         }
132         // ensure file is open
133
file.open( openFlags, access, SmbFile.ATTR_NORMAL, 0 );
134
135         /*
136          * Read AndX Request / Response
137          */

138
139         if( file.log.level >= 4 )
140             file.log.println( "read: fid=" + file.fid + ",off=" + off + ",len=" + len );
141
142         SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off );
143
144         if( file.type == SmbFile.TYPE_NAMED_PIPE ) {
145             response.responseTimeout = 0;
146         }
147
148         int r, n;
149         do {
150             r = len > readSize ? readSize : len;
151
152             if( file.log.level >= 4 )
153                 file.log.println( "read: len=" + len + ",r=" + r + ",fp=" + fp );
154
155             try {
156 SmbComReadAndX request = new SmbComReadAndX( file.fid, fp, r, null );
157 if( file.type == SmbFile.TYPE_NAMED_PIPE ) {
158     request.minCount = request.maxCount = request.remaining = 1024;
159 }
160                 file.send( request, response );
161             } catch( SmbException se ) {
162                 if( file.type == SmbFile.TYPE_NAMED_PIPE &&
163                         se.getNtStatus() == NtStatus.NT_STATUS_PIPE_BROKEN ) {
164                     return -1;
165                 }
166                 throw se;
167             }
168             if(( n = response.dataLength ) <= 0 ) {
169                 return (int)((fp - start) > 0L ? fp - start : -1);
170             }
171             fp += n;
172             len -= n;
173             response.off += n;
174         } while( len > 0 && n == r );
175
176         return (int)(fp - start);
177     }
178 /**
179  * This stream class is unbuffered. Therefore this method will always
180  * return 0 for streams connected to regular files. However, a
181  * stream created from a Named Pipe this method will query the server using a
182  * "peek named pipe" operation and return the number of available bytes
183  * on the server.
184  */

185     public int available() throws IOException JavaDoc {
186         SmbNamedPipe pipe;
187         TransPeekNamedPipe req;
188         TransPeekNamedPipeResponse resp;
189
190         if( file.type != SmbFile.TYPE_NAMED_PIPE ) {
191             return 0;
192         }
193
194         pipe = (SmbNamedPipe)file;
195         file.open(SmbFile.O_EXCL, pipe.pipeType & 0xFF0000, SmbFile.ATTR_NORMAL, 0 );
196
197         req = new TransPeekNamedPipe( file.unc, file.fid );
198         resp = new TransPeekNamedPipeResponse( pipe );
199
200         pipe.send( req, resp );
201         if( resp.status == TransPeekNamedPipeResponse.STATUS_DISCONNECTED ||
202                 resp.status == TransPeekNamedPipeResponse.STATUS_SERVER_END_CLOSED ) {
203             file.opened = false;
204             return 0;
205         }
206         return resp.available;
207     }
208 /**
209  * Skip n bytes of data on this stream. This operation will not result
210  * in any IO with the server. Unlink <tt>InputStream</tt> value less than
211  * the one provided will not be returned if it exceeds the end of the file
212  * (if this is a problem let us know).
213  */

214     public long skip( long n ) throws IOException JavaDoc {
215         if (n > 0) {
216             fp += n;
217             return n;
218         }
219         return 0;
220     }
221 }
222
223
Popular Tags