FileDocCategorySizeDatePackage
Encdec.javaAPI DocJCIFS 1.3.17 API11484Tue Oct 18 15:26:24 BST 2011jcifs.util

Encdec

public class Encdec extends Object

Fields Summary
public static final long
MILLISECONDS_BETWEEN_1970_AND_1601
public static final long
SEC_BETWEEEN_1904_AND_1970
public static final int
TIME_1970_SEC_32BE
public static final int
TIME_1970_SEC_32LE
public static final int
TIME_1904_SEC_32BE
public static final int
TIME_1904_SEC_32LE
public static final int
TIME_1601_NANOS_64LE
public static final int
TIME_1601_NANOS_64BE
public static final int
TIME_1970_MILLIS_64BE
public static final int
TIME_1970_MILLIS_64LE
Constructors Summary
Methods Summary
public static doubledec_doublebe(byte[] src, int si)

        return Double.longBitsToDouble( dec_uint64be( src, si ));
    
public static doubledec_doublele(byte[] src, int si)

        return Double.longBitsToDouble( dec_uint64le( src, si ));
    
public static floatdec_floatbe(byte[] src, int si)

        return Float.intBitsToFloat( dec_uint32be( src, si ));
    
public static floatdec_floatle(byte[] src, int si)

        return Float.intBitsToFloat( dec_uint32le( src, si ));
    
public static java.util.Datedec_time(byte[] src, int si, int enc)

        long t;
    
        switch( enc ) {
            case TIME_1970_SEC_32BE:
                return new Date( dec_uint32be( src, si ) * 1000L );
            case TIME_1970_SEC_32LE:
                return new Date( dec_uint32le( src, si ) * 1000L );
            case TIME_1904_SEC_32BE:
                return new Date((( dec_uint32be( src, si ) & 0xFFFFFFFFL) -
                    SEC_BETWEEEN_1904_AND_1970 ) * 1000L );
            case TIME_1904_SEC_32LE:
                return new Date((( dec_uint32le( src, si ) & 0xFFFFFFFFL) -
                    SEC_BETWEEEN_1904_AND_1970 ) * 1000L );
            case TIME_1601_NANOS_64BE:
                t = dec_uint64be( src, si );
                return new Date( t / 10000L - MILLISECONDS_BETWEEN_1970_AND_1601);
            case TIME_1601_NANOS_64LE:
                t = dec_uint64le( src, si );
                return new Date( t / 10000L - MILLISECONDS_BETWEEN_1970_AND_1601);
            case TIME_1970_MILLIS_64BE:
                return new Date( dec_uint64be( src, si ));
            case TIME_1970_MILLIS_64LE:
                return new Date( dec_uint64le( src, si ));
            default:
                throw new IllegalArgumentException( "Unsupported time encoding" );
        }
    
public static java.lang.Stringdec_ucs2le(byte[] src, int si, int slim, char[] buf)

        int bi;

        for( bi = 0; (si + 1) < slim; bi++, si += 2 ) {
            buf[bi] = (char)dec_uint16le( src, si );
            if( buf[bi] == '\0" ) {
                break;
            }
        }

        return new String( buf, 0, bi );
    
public static shortdec_uint16be(byte[] src, int si)

        return (short)(((src[si] & 0xFF) << 8) | (src[si + 1] & 0xFF));
    
public static shortdec_uint16le(byte[] src, int si)

        return (short)((src[si] & 0xFF) | ((src[si + 1] & 0xFF) << 8));
    
public static intdec_uint32be(byte[] src, int si)

        return ((src[si] & 0xFF) << 24) | ((src[si + 1] & 0xFF) << 16) |
               ((src[si + 2] & 0xFF) << 8) | (src[si + 3] & 0xFF);
    
public static intdec_uint32le(byte[] src, int si)

        return (src[si] & 0xFF) | ((src[si + 1] & 0xFF) << 8) |
               ((src[si + 2] & 0xFF) << 16) | ((src[si + 3] & 0xFF) << 24);
    
public static longdec_uint64be(byte[] src, int si)

        long l;
        l = dec_uint32be( src, si ) & 0xFFFFFFFFL;
        l <<= 32L;
        l |= dec_uint32be( src, si + 4 ) & 0xFFFFFFFFL;
        return l;
    
public static longdec_uint64le(byte[] src, int si)

        long l;
        l = dec_uint32le( src, si + 4 ) & 0xFFFFFFFFL;
        l <<= 32L;
        l |= dec_uint32le( src, si ) & 0xFFFFFFFFL;
        return l;
    
public static java.lang.Stringdec_utf8(byte[] src, int si, int slim)

        char[] uni = new char[slim - si];
        int ui, ch;

        for( ui = 0; si < slim && (ch = src[si++] & 0xFF) != 0; ui++ ) {
            if( ch < 0x80 ) {
                uni[ui] = (char)ch;
            } else if((ch & 0xE0) == 0xC0 ) {
                if((slim - si) < 2 ) {
                    break;
                }
                uni[ui] = (char)((ch & 0x1F) << 6);
                ch = src[si++] & 0xFF;
                uni[ui] |= ch & 0x3F;
                if ((ch & 0xC0) != 0x80 || uni[ui] < 0x80 ) {
                    throw new IOException( "Invalid UTF-8 sequence" );
                }
            } else if((ch & 0xF0) == 0xE0 ) {
                if((slim - si) < 3 ) {
                    break;
                }
                uni[ui]  = (char)((ch & 0x0F) << 12);
                ch = src[si++] & 0xFF;
                if ((ch & 0xC0) != 0x80 ) {
                    throw new IOException( "Invalid UTF-8 sequence" );
                } else {
                    uni[ui] |= (ch & 0x3F) << 6;
                    ch = src[si++] & 0xFF;
                    uni[ui] |=  ch & 0x3F;
                    if ((ch & 0xC0) != 0x80 || uni[ui] < 0x800) {
                        throw new IOException( "Invalid UTF-8 sequence" );
                    }
                }
            } else {
                throw new IOException( "Unsupported UTF-8 sequence" );
            }
        }

        return new String( uni, 0, ui );
    
public static intenc_doublebe(double d, byte[] dst, int di)

        return enc_uint64be( Double.doubleToLongBits( d ), dst, di );
    
public static intenc_doublele(double d, byte[] dst, int di)

        return enc_uint64le( Double.doubleToLongBits( d ), dst, di );
    
public static intenc_floatbe(float f, byte[] dst, int di)

        return enc_uint32be( Float.floatToIntBits( f ), dst, di );
    
public static intenc_floatle(float f, byte[] dst, int di)

        return enc_uint32le( Float.floatToIntBits( f ), dst, di );
    
public static intenc_time(java.util.Date date, byte[] dst, int di, int enc)

        long t;

        switch( enc ) {
            case TIME_1970_SEC_32BE:
                return enc_uint32be( (int)(date.getTime() / 1000L), dst, di );
            case TIME_1970_SEC_32LE:
                return enc_uint32le( (int)(date.getTime() / 1000L), dst, di );
            case TIME_1904_SEC_32BE:
                return enc_uint32be( (int)((date.getTime() / 1000L +
                    SEC_BETWEEEN_1904_AND_1970) & 0xFFFFFFFF), dst, di );
            case TIME_1904_SEC_32LE:
                return enc_uint32le( (int)((date.getTime() / 1000L +
                    SEC_BETWEEEN_1904_AND_1970) & 0xFFFFFFFF), dst, di );
            case TIME_1601_NANOS_64BE:
                t = (date.getTime() + MILLISECONDS_BETWEEN_1970_AND_1601) * 10000L;
                return enc_uint64be( t, dst, di );
            case TIME_1601_NANOS_64LE:
                t = (date.getTime() + MILLISECONDS_BETWEEN_1970_AND_1601) * 10000L;
                return enc_uint64le( t, dst, di );
            case TIME_1970_MILLIS_64BE:
                return enc_uint64be( date.getTime(), dst, di );
            case TIME_1970_MILLIS_64LE:
                return enc_uint64le( date.getTime(), dst, di );
            default:
                throw new IllegalArgumentException( "Unsupported time encoding" );
        }
    
public static intenc_uint16be(short s, byte[] dst, int di)


    /* Encode integers
     */

               
        dst[di++] = (byte)((s >> 8) & 0xFF);
        dst[di] = (byte)(s & 0xFF);
        return 2;
    
public static intenc_uint16le(short s, byte[] dst, int di)

        dst[di++] = (byte)(s & 0xFF);
        dst[di] = (byte)((s >> 8) & 0xFF);
        return 2;
    
public static intenc_uint32be(int i, byte[] dst, int di)

        dst[di++] = (byte)((i >> 24) & 0xFF);
        dst[di++] = (byte)((i >> 16) & 0xFF);
        dst[di++] = (byte)((i >> 8) & 0xFF);
        dst[di] = (byte)(i & 0xFF);
        return 4;
    
public static intenc_uint32le(int i, byte[] dst, int di)

        dst[di++] = (byte)(i & 0xFF);
        dst[di++] = (byte)((i >> 8) & 0xFF);
        dst[di++] = (byte)((i >> 16) & 0xFF);
        dst[di] = (byte)((i >> 24) & 0xFF);
        return 4;
    
public static intenc_uint64be(long l, byte[] dst, int di)

        enc_uint32be( (int)(l & 0xFFFFFFFFL), dst, di + 4 );
        enc_uint32be( (int)(( l >> 32L ) & 0xFFFFFFFFL), dst, di );
        return 8;
    
public static intenc_uint64le(long l, byte[] dst, int di)

        enc_uint32le( (int)(l & 0xFFFFFFFFL), dst, di );
        enc_uint32le( (int)(( l >> 32L ) & 0xFFFFFFFFL), dst, di + 4 );
        return 8;
    
public static intenc_utf8(java.lang.String str, byte[] dst, int di, int dlim)

        int start = di, ch;
        int strlen = str.length();

        for( int i = 0; di < dlim && i < strlen; i++ ) {
            ch = str.charAt( i );
            if ((ch >= 0x0001) && (ch <= 0x007F)) {
                dst[di++] = (byte)ch;
            } else if (ch > 0x07FF) {
                if((dlim - di) < 3 ) {
                    break;
                }
                dst[di++] = (byte)(0xE0 | ((ch >> 12) & 0x0F)); 
                dst[di++] = (byte)(0x80 | ((ch >>  6) & 0x3F)); 
                dst[di++] = (byte)(0x80 | ((ch >>  0) & 0x3F)); 
            } else {
                if((dlim - di) < 2 ) {
                    break;
                }
                dst[di++] = (byte)(0xC0 | ((ch >>  6) & 0x1F)); 
                dst[di++] = (byte)(0x80 | ((ch >>  0) & 0x3F)); 
            }
        }

        return di - start;