public InputStream getInputStream(final ClientIdentification.Agent agent, final String username, final String pass) throws IOException { if (isFile()) return new BufferedInputStream(new FileInputStream(getFSFile())); if (isSMB()) return new BufferedInputStream(new SmbFileInputStream(getSmbFile())); if (isFTP()) { FTPStorageFactory client = new FTPStorageFactory(this.host, this.port < 0 ? 21 : this.port, username, pass, false); Asset<byte[]> asset = client.getStorage().load(this.path); return new ByteArrayInputStream(asset.getPayload()); } if (isHTTP() || isHTTPS()) { // TODO: add agent, user and pass return new ByteArrayInputStream(ClientConnection.load(this.toString())); } return null; }
/** * Reads up to b.length bytes of data from this input stream into an array of bytes. * * @throws IOException if a network error occurs */ public int read( byte[] b ) throws IOException { return read( b, 0, b.length ); }
/** * Reads up to len bytes of data from this input stream into an array of bytes. * * @throws IOException if a network error occurs */ public int read( byte[] b, int off, int len ) throws IOException { return readDirect(b, off, len); } public int readDirect( byte[] b, int off, int len ) throws IOException {
protected void doFile ( HttpServletRequest req, HttpServletResponse resp, SmbFile file ) throws IOException { byte[] buf = new byte[8192]; @SuppressWarnings ( "resource" ) ServletOutputStream out = resp.getOutputStream(); String url; int n; try ( SmbFileInputStream in = new SmbFileInputStream(file) ) { url = file.getLocator().getPath(); resp.setContentType("text/plain"); resp.setContentType(URLConnection.guessContentTypeFromName(url)); resp.setHeader("Content-Length", file.length() + ""); resp.setHeader("Accept-Ranges", "Bytes"); while ( ( n = in.read(buf) ) != -1 ) { out.write(buf, 0, n); } } }
try fis = new SmbFileInputStream("smb://DOMAIN;USERNAME:PASSWORD@SERVER/SHARE/filename.txt"); while ((length = fis.read(buffer)) != -1) fis.close();
protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException { int off, flags, length; if (buf.length < max_recv) throw new IllegalArgumentException("buffer too small"); if (isStart && !isDirect) { // start of new frag, do trans off = in.read(buf, 0, 1024); } else { off = in.readDirect(buf, 0, buf.length); } if (buf[0] != 5 && buf[1] != 0) throw new IOException("Unexpected DCERPC PDU header"); flags = buf[3] & 0xFF; // next read is start of new frag isStart = (flags & DCERPC_LAST_FRAG) == DCERPC_LAST_FRAG; length = Encdec.dec_uint16le(buf, 8); if (length > max_recv) throw new IOException("Unexpected fragment length: " + length); while (off < length) { off += in.readDirect(buf, off, length - off); } } public void close() throws IOException {
/** * Closes this input stream and releases any system resources associated with the stream. * * @throws IOException if a network error occurs */ public void close() throws IOException { try { file.close(); tmp = null; } catch (SmbException se) { throw seToIoe(se); } }
try ( SmbFileHandleImpl fd = ensureOpen(); SmbTreeHandleImpl th = fd.getTree() ) { return -1; throw seToIoe(se);
SmbFileInputStream ( SmbFile file, int openFlags, int access, int sharing, boolean unshared ) throws SmbException { this.file = file; this.unsharedFile = unshared; this.openFlags = openFlags; this.access = access; try ( SmbTreeHandleInternal th = file.ensureTreeConnected() ) { this.smb2 = th.isSMB2(); if ( file.getType() != SmbConstants.TYPE_NAMED_PIPE ) { try ( SmbFileHandle h = ensureOpen() ) {} this.openFlags &= ~ ( SmbConstants.O_CREAT | SmbConstants.O_TRUNC ); } init(th); } catch ( CIFSException e ) { throw SmbException.wrap(e); } }
protected void doFile( HttpServletRequest req, HttpServletResponse resp, SmbFile file ) throws IOException { byte[] buf = new byte[8192]; SmbFileInputStream in; ServletOutputStream out; String url, type; int n; in = new SmbFileInputStream( file ); out = resp.getOutputStream(); url = file.getPath(); resp.setContentType( "text/plain" ); if(( n = url.lastIndexOf( '.' )) > 0 && ( type = url.substring( n + 1 )) != null && type.length() > 1 && type.length() < 6 ) { resp.setContentType( mimeMap.getMimeType( type )); } resp.setHeader( "Content-Length", file.length() + "" ); resp.setHeader( "Accept-Ranges", "Bytes" ); while(( n = in.read( buf )) != -1 ) { out.write( buf, 0, n ); } } protected int compareNames( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException { int off, flags, length; if (buf.length < max_recv) throw new IllegalArgumentException("buffer too small"); if (isStart && !isDirect) { // start of new frag, do trans off = in.read(buf, 0, 1024); } else { off = in.readDirect(buf, 0, buf.length); } if (buf[0] != 5 && buf[1] != 0) throw new IOException("Unexpected DCERPC PDU header"); flags = buf[3] & 0xFF; // next read is start of new frag isStart = (flags & DCERPC_LAST_FRAG) == DCERPC_LAST_FRAG; length = Encdec.dec_uint16le(buf, 8); if (length > max_recv) throw new IOException("Unexpected fragment length: " + length); while (off < length) { off += in.readDirect(buf, off, length - off); } } public void close() throws IOException {
/** * Closes this input stream and releases any system resources associated with the stream. * * @throws IOException if a network error occurs */ public void close() throws IOException { try { file.close(); tmp = null; } catch (SmbException se) { throw seToIoe(se); } }
try ( SmbFileHandleImpl fd = ensureOpen(); SmbTreeHandleImpl th = fd.getTree() ) { return -1; throw seToIoe(se);
SmbFileInputStream ( SmbFile file, int openFlags, int access, int sharing, boolean unshared ) throws SmbException { this.file = file; this.unsharedFile = unshared; this.openFlags = openFlags; this.access = access; try ( SmbTreeHandleInternal th = file.ensureTreeConnected() ) { this.smb2 = th.isSMB2(); if ( file.getType() != SmbConstants.TYPE_NAMED_PIPE ) { try ( SmbFileHandle h = ensureOpen() ) {} this.openFlags &= ~ ( SmbConstants.O_CREAT | SmbConstants.O_TRUNC ); } init(th); } catch ( CIFSException e ) { throw SmbException.wrap(e); } }
/** * This URLConnection method just returns a new <tt>SmbFileInputStream</tt> created with this file. * * @throws IOException * thrown by <tt>SmbFileInputStream</tt> constructor */ @Override public InputStream getInputStream () throws IOException { return new SmbFileInputStream(this); }
protected void doFile( HttpServletRequest req, HttpServletResponse resp, SmbFile file ) throws IOException { byte[] buf = new byte[8192]; SmbFileInputStream in; ServletOutputStream out; String url, type; int n; in = new SmbFileInputStream( file ); out = resp.getOutputStream(); url = file.getPath(); resp.setContentType( "text/plain" ); if(( n = url.lastIndexOf( '.' )) > 0 && ( type = url.substring( n + 1 )) != null && type.length() > 1 && type.length() < 6 ) { resp.setContentType( mimeMap.getMimeType( type )); } resp.setHeader( "Content-Length", file.length() + "" ); resp.setHeader( "Accept-Ranges", "Bytes" ); while(( n = in.read( buf )) != -1 ) { out.write( buf, 0, n ); } } protected int compareNames( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
/** * Reads a byte of data from this input stream. * * @throws IOException if a network error occurs */ public int read() throws IOException { // need oplocks to cache otherwise use BufferedInputStream if( read( tmp, 0, 1 ) == -1 ) { return -1; } return tmp[0] & 0xFF; }
protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException { int off, flags, length; if (buf.length < max_recv) throw new IllegalArgumentException("buffer too small"); if (isStart && !isDirect) { // start of new frag, do trans off = in.read(buf, 0, 1024); } else { off = in.readDirect(buf, 0, buf.length); } if (buf[0] != 5 && buf[1] != 0) throw new IOException("Unexpected DCERPC PDU header"); flags = buf[3] & 0xFF; // next read is start of new frag isStart = (flags & DCERPC_LAST_FRAG) == DCERPC_LAST_FRAG; length = Encdec.dec_uint16le(buf, 8); if (length > max_recv) throw new IOException("Unexpected fragment length: " + length); while (off < length) { off += in.readDirect(buf, off, length - off); } } public void close() throws IOException {