FileDocCategorySizeDatePackage
SmbRandomAccessFile.javaAPI DocJCIFS 1.3.17 API11449Tue Oct 18 15:26:24 BST 2011jcifs.smb

SmbRandomAccessFile

public class SmbRandomAccessFile extends Object implements DataInput, DataOutput

Fields Summary
private static final int
WRITE_OPTIONS
private SmbFile
file
private long
fp
private int
openFlags
private int
access
private int
readSize
private int
writeSize
private int
ch
private int
options
private byte[]
tmp
private SmbComWriteAndXResponse
write_andx_resp
Constructors Summary
public SmbRandomAccessFile(String url, String mode, int shareAccess)


            
                
        this( new SmbFile( url, "", null, shareAccess ), mode );
    
public SmbRandomAccessFile(SmbFile file, String mode)

        this.file = file;
        if( mode.equals( "r" )) {
            this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDONLY;
        } else if( mode.equals( "rw" )) {
            this.openFlags = SmbFile.O_CREAT | SmbFile.O_RDWR | SmbFile.O_APPEND;
            write_andx_resp = new SmbComWriteAndXResponse();
            options = WRITE_OPTIONS;
            access = SmbConstants.FILE_READ_DATA | SmbConstants.FILE_WRITE_DATA;
        } else {
            throw new IllegalArgumentException( "Invalid mode" );
        }
        file.open( openFlags, access, SmbFile.ATTR_NORMAL, options );
        readSize = file.tree.session.transport.rcv_buf_size - 70;
        writeSize = file.tree.session.transport.snd_buf_size - 70;
        fp = 0L;
    
Methods Summary
public voidclose()

        file.close();
    
public longgetFilePointer()

        return fp;
    
public longlength()

        return file.length();
    
public intread()

        if( read( tmp, 0, 1 ) == -1 ) {
            return -1;
        }
        return tmp[0] & 0xFF;
    
public intread(byte[] b)

        return read( b, 0, b.length );
    
public intread(byte[] b, int off, int len)

        if( len <= 0 ) {
            return 0;
        }
        long start = fp;

        // ensure file is open
        if( file.isOpen() == false ) {
            file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
        }

        int r, n;
        SmbComReadAndXResponse response = new SmbComReadAndXResponse( b, off );
        do {
            r = len > readSize ? readSize : len;
            file.send( new SmbComReadAndX( file.fid, fp, r, null ), response );
            if(( n = response.dataLength ) <= 0 ) {
                return (int)((fp - start) > 0L ? fp - start : -1);
            }
            fp += n;
            len -= n;
            response.off += n;
        } while( len > 0 && n == r );

        return (int)(fp - start);
    
public final booleanreadBoolean()

        if((read( tmp, 0, 1 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return tmp[0] != (byte)0x00;
    
public final bytereadByte()

        if((read( tmp, 0, 1 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return tmp[0];
    
public final charreadChar()

        if((read( tmp, 0, 2 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return (char)Encdec.dec_uint16be( tmp, 0 );
    
public final doublereadDouble()

        if((read( tmp, 0, 8 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_doublebe( tmp, 0 );
    
public final floatreadFloat()

        if((read( tmp, 0, 4 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_floatbe( tmp, 0 );
    
public final voidreadFully(byte[] b)

        readFully( b, 0, b.length );
    
public final voidreadFully(byte[] b, int off, int len)

        int n = 0, count;

        do {    
            count = this.read( b, off + n, len - n );
            if( count < 0 ) throw new SmbException( "EOF" );
            n += count;
            fp += count;
        } while( n < len );
    
public final intreadInt()

        if((read( tmp, 0, 4 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_uint32be( tmp, 0 );
    
public final java.lang.StringreadLine()

        StringBuffer input = new StringBuffer();
        int c = -1;
        boolean eol = false;

        while (!eol) {
            switch( c = read() ) {
                case -1:
                case '\n":
                    eol = true;
                    break;
                case '\r":
                    eol = true;
                    long cur = fp;
                    if( read() != '\n" ) {
                        fp = cur;
                    }
                    break;
                default:
                    input.append( (char)c );
                    break;
            }
        }

        if ((c == -1) && (input.length() == 0)) {
            return null;
        }

        return input.toString();
    
public final longreadLong()

        if((read( tmp, 0, 8 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_uint64be( tmp, 0 );
    
public final shortreadShort()

        if((read( tmp, 0, 2 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_uint16be( tmp, 0 );
    
public final java.lang.StringreadUTF()

        int size = readUnsignedShort();
        byte[] b = new byte[size];
        read( b, 0, size );
        try {
            return Encdec.dec_utf8( b, 0, size );
        } catch( IOException ioe ) {
            throw new SmbException( "", ioe );
        }
    
public final intreadUnsignedByte()

        if((read( tmp, 0, 1 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return tmp[0] & 0xFF;
    
public final intreadUnsignedShort()

        if((read( tmp, 0, 2 )) < 0 ) {
            throw new SmbException( "EOF" );
        }
        return Encdec.dec_uint16be( tmp, 0 ) & 0xFFFF;
    
public voidseek(long pos)

        fp = pos;
    
public voidsetLength(long newLength)

        // ensure file is open
        if( file.isOpen() == false ) {
            file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
        }
        SmbComWriteResponse rsp = new SmbComWriteResponse();
        file.send( new SmbComWrite( file.fid, (int)(newLength & 0xFFFFFFFFL), 0, tmp, 0, 0 ), rsp );
    
public intskipBytes(int n)

        if (n > 0) {
            fp += n;
            return n;
        }
        return 0;
    
public voidwrite(byte[] b)

        write( b, 0, b.length );
    
public voidwrite(byte[] b, int off, int len)

        if( len <= 0 ) {
            return;
        }

        // ensure file is open
        if( file.isOpen() == false ) {
            file.open( openFlags, 0, SmbFile.ATTR_NORMAL, options );
        }

        int w;
        do {
            w = len > writeSize ? writeSize : len;
            file.send( new SmbComWriteAndX( file.fid, fp, len - w, b, off, w, null ), write_andx_resp );
            fp += write_andx_resp.count;
            len -= write_andx_resp.count;
            off += write_andx_resp.count;
        } while( len > 0 );
    
public voidwrite(int b)

        tmp[0] = (byte)b;
        write( tmp, 0, 1 );
    
public final voidwriteBoolean(boolean v)

        tmp[0] = (byte)(v ? 1 : 0);
        write( tmp, 0, 1 );
    
public final voidwriteByte(int v)

        tmp[0] = (byte)v;
        write( tmp, 0, 1 );
    
public final voidwriteBytes(java.lang.String s)

        byte[] b = s.getBytes();
        write( b, 0, b.length );
    
public final voidwriteChar(int v)

        Encdec.enc_uint16be( (short)v, tmp, 0 );
        write( tmp, 0, 2 );
    
public final voidwriteChars(java.lang.String s)

        int clen = s.length();
        int blen = 2 * clen;
        byte[] b = new byte[blen];
        char[] c = new char[clen];
        s.getChars( 0, clen, c, 0 );
        for( int i = 0, j = 0; i < clen; i++ ) {
            b[j++] = (byte)(c[i] >>> 8);
            b[j++] = (byte)(c[i] >>> 0);
        }
        write( b, 0, blen );
    
public final voidwriteDouble(double v)

        Encdec.enc_doublebe( v, tmp, 0 );
        write( tmp, 0, 8 );
    
public final voidwriteFloat(float v)

        Encdec.enc_floatbe( v, tmp, 0 );
        write( tmp, 0, 4 );
    
public final voidwriteInt(int v)

        Encdec.enc_uint32be( v, tmp, 0 );
        write( tmp, 0, 4 );
    
public final voidwriteLong(long v)

        Encdec.enc_uint64be( v, tmp, 0 );
        write( tmp, 0, 8 );
    
public final voidwriteShort(int v)

        Encdec.enc_uint16be( (short)v, tmp, 0 );
        write( tmp, 0, 2 );
    
public final voidwriteUTF(java.lang.String str)

        int len = str.length();
        int ch, size = 0;
        byte[] dst;

        for( int i = 0; i < len; i++ ) {
            ch = str.charAt( i );
            size += ch > 0x07F ? (ch > 0x7FF ? 3 : 2) : 1;
        }
        dst = new byte[size];
        writeShort( size );
        try {
            Encdec.enc_utf8( str, dst, 0, size );
        } catch( IOException ioe ) {
            throw new SmbException( "", ioe );
        }
        write( dst, 0, size );