⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialutils.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    o.deserialize( din );
	    }
	else
	    o = null;
	deserializeTrailer( cl, din );
	return o;
	}

    /// Utility routine to deserialize a String.
    public static String deserializeString( DataInputStream din ) throws IOException
	{
	String str;
	if ( deserializeHeaderString( tagString, din ) )
	    str = din.readUTF();
	else
	    str = null;
	deserializeTrailerString( tagString, din );
	return str;
	}

    /// Utility routine to deserialize a boolean.
    public static boolean deserializeBoolean( DataInputStream din ) throws IOException
	{
	return din.readBoolean();
	}

    /// Utility routine to deserialize a byte.
    public static byte deserializeByte( DataInputStream din ) throws IOException
	{
	return din.readByte();
	}

    /// Utility routine to deserialize a char.
    public static char deserializeChar( DataInputStream din ) throws IOException
	{
	return din.readChar();
	}

    /// Utility routine to deserialize a short.
    public static short deserializeShort( DataInputStream din ) throws IOException
	{
	return din.readShort();
	}

    /// Utility routine to deserialize a int.
    public static int deserializeInt( DataInputStream din ) throws IOException
	{
	return din.readInt();
	}

    /// Utility routine to deserialize a long.
    public static long deserializeLong( DataInputStream din ) throws IOException
	{
	return din.readLong();
	}

    /// Utility routine to deserialize a float.
    public static float deserializeFloat( DataInputStream din ) throws IOException
	{
	return din.readFloat();
	}

    /// Utility routine to deserialize a double.
    public static double deserializeDouble( DataInputStream din ) throws IOException
	{
	return din.readDouble();
	}

    /// Utility routine to deserialize an array of objects.
    public static Serializable[] deserializeArrayObject( DataInputStream din ) throws IOException
	{
	Serializable[] ao;
	if ( deserializeHeaderString( tagArrayObject, din ) )
	    {
	    short len = din.readShort();
	    ao = new Serializable[len];
	    for ( int i = 0; i < len; ++i )
		ao[i] = deserializeObject( null, din );	// doesn't actually work
	    }
	else
	    ao = null;
	deserializeTrailerString( tagArrayObject, din );
	return ao;
	}

    /// Utility routine to deserialize an array of bytes.
    public static byte[] deserializeArrayByte( DataInputStream din ) throws IOException
	{
	byte[] ab;
	if ( deserializeHeaderString( tagArrayByte, din ) )
	    {
	    short len = din.readShort();
	    ab = new byte[len];
	    for ( int i = 0; i < len; ++i )
		ab[i] = din.readByte();
	    }
	else
	    ab = null;
	deserializeTrailerString( tagArrayByte, din );
	return ab;
	}

    /// Utility routine to deserialize an array of chars.
    public static char[] deserializeArrayChar( DataInputStream din ) throws IOException
	{
	char[] ac;
	if ( deserializeHeaderString( tagArrayChar, din ) )
	    {
	    short len = din.readShort();
	    ac = new char[len];
	    for ( int i = 0; i < len; ++i )
		ac[i] = din.readChar();
	    }
	else
	    ac = null;
	deserializeTrailerString( tagArrayChar, din );
	return ac;
	}

    /// Utility routine to deserialize an array of shorts.
    public static short[] deserializeArrayShort( DataInputStream din ) throws IOException
	{
	short[] as;
	if ( deserializeHeaderString( tagArrayShort, din ) )
	    {
	    short len = din.readShort();
	    as = new short[len];
	    for ( int i = 0; i < len; ++i )
		as[i] = din.readShort();
	    }
	else
	    as = null;
	deserializeTrailerString( tagArrayShort, din );
	return as;
	}

    /// Utility routine to deserialize an array of ints.
    public static int[] deserializeArrayInt( DataInputStream din ) throws IOException
	{
	int[] ai;
	if ( deserializeHeaderString( tagArrayInt, din ) )
	    {
	    short len = din.readShort();
	    ai = new int[len];
	    for ( int i = 0; i < len; ++i )
		ai[i] = din.readInt();
	    }
	else
	    ai = null;
	deserializeTrailerString( tagArrayInt, din );
	return ai;
	}

    /// Utility routine to deserialize an array of longs.
    public static long[] deserializeArrayLong( DataInputStream din ) throws IOException
	{
	long[] al;
	if ( deserializeHeaderString( tagArrayLong, din ) )
	    {
	    short len = din.readShort();
	    al = new long[len];
	    for ( int i = 0; i < len; ++i )
		al[i] = din.readLong();
	    }
	else
	    al = null;
	deserializeTrailerString( tagArrayLong, din );
	return al;
	}

    /// Utility routine to deserialize an array of floats.
    public static float[] deserializeArrayFloat( DataInputStream din ) throws IOException
	{
	float[] af;
	if ( deserializeHeaderString( tagArrayFloat, din ) )
	    {
	    short len = din.readShort();
	    af = new float[len];
	    for ( int i = 0; i < len; ++i )
		af[i] = din.readFloat();
	    }
	else
	    af = null;
	deserializeTrailerString( tagArrayFloat, din );
	return af;
	}

    /// Utility routine to deserialize an array of doubles.
    public static double[] deserializeArrayDouble( DataInputStream din ) throws IOException
	{
	double[] ad;
	if ( deserializeHeaderString( tagArrayDouble, din ) )
	    {
	    short len = din.readShort();
	    ad = new double[len];
	    for ( int i = 0; i < len; ++i )
		ad[i] = din.readDouble();
	    }
	else
	    ad = null;
	deserializeTrailerString( tagArrayDouble, din );
	return ad;
	}


    /// Test routine.
    public static void main( String[] args )
	{
	if ( args.length == 1 && args[0].equals( "-s" ) )
	    {
	    SerialUtilsTest t;
	    long[] al1 = { 0, 1, 2 };
	    long[] al2 = { 3, 4, 5, 6 };
	    t = new SerialUtilsTest( null, "Yo", (byte) 23, '@', (short) 23456, 234567890, 234567890123L, 23.4567F, 23.4567890123D, al2 );
	    t = new SerialUtilsTest( t, "Hi there", (byte) 123, 'x', (short) 12345, 123456789, 123456789012L, 123.456F, 123.456789012D, al1 );
	    DataOutputStream dout = new DataOutputStream( System.out );
	    try
		{
		SerialUtils.serializeObject( t, dout );
		dout.flush();
		}
	    catch ( IOException e )
		{
		System.err.println( e.toString() );
		e.printStackTrace();
		System.exit( 1 );
		}
	    }
	else if ( args.length == 1 && args[0].equals( "-d" ) )
	    {
	    SerialUtilsTest t = new SerialUtilsTest();
	    DataInputStream din = new DataInputStream( System.in );
	    try
		{
		t = (SerialUtilsTest) SerialUtils.deserializeObject(
		    t.getClass(), din );
		}
	    catch ( IOException e )
		{
		System.err.println( e.toString() );
		e.printStackTrace();
		System.exit( 1 );
		}
	    System.out.println( t.toString() );
	    }
	else
	    {
	    System.err.println( "usage:  SerialUtils -s|-d" );
	    System.exit( 1 );
	    }
	System.exit( 0 );
	}

    }


/// Sample/test class.

class SerialUtilsTest implements Acme.Serializable
    {

    SerialUtilsTest sub;
    String str;
    byte b;
    char c;
    short s;
    int i;
    long l;
    float f;
    double d;
    long[] al;

    /// Real constructor.
    public SerialUtilsTest( SerialUtilsTest sub, String str, byte b, char c, short s, int i, long l, float f, double d, long[] al )
	{
	this.sub = sub;
	this.str = str;
	this.b = b;
	this.c = c;
	this.s = s;
	this.i = i;
	this.l = l;
	this.f = f;
	this.d = d;
	this.al = al;
	}


    /// No-args constructor, makes a blank instance to be filled in by the
    // deserializer.
    public SerialUtilsTest()
	{
	}

    /// Version routine.
    public String getVersion()
	{
	return "1";
	}

    /// Serialize routine for the interface.
    public void serialize( DataOutputStream dout ) throws IOException
	{
	System.err.println( "Serializing..." );
	SerialUtils.serializeObject( sub, dout );
	SerialUtils.serializeString( str, dout );
	SerialUtils.serializeByte( b, dout );
	SerialUtils.serializeChar( c, dout );
	SerialUtils.serializeShort( s, dout );
	SerialUtils.serializeInt( i, dout );
	SerialUtils.serializeLong( l, dout );
	SerialUtils.serializeFloat( f, dout );
	SerialUtils.serializeDouble( d, dout );
	SerialUtils.serializeArrayLong( al, dout );
	System.err.println( "Done serializing." );
	}

    /// Deserialize routine for the interface.
    public void deserialize( DataInputStream din ) throws IOException
	{
	System.err.println( "Deserializing..." );
	sub = (SerialUtilsTest) SerialUtils.deserializeObject(
	    this.getClass(), din );
	str = SerialUtils.deserializeString( din );
	b = SerialUtils.deserializeByte( din );
	c = SerialUtils.deserializeChar( din );
	s = SerialUtils.deserializeShort( din );
	i = SerialUtils.deserializeInt( din );
	l = SerialUtils.deserializeLong( din );
	f = SerialUtils.deserializeFloat( din );
	d = SerialUtils.deserializeDouble( din );
	al = SerialUtils.deserializeArrayLong( din );
	System.err.println( "Done deserializing." );
	}

    /// String output routine.
    public String toString()
	{
	return
	    "[" +
	    this.getClass().getName() + " " +
	    sub + " " +
	    str.toString() + " " +
	    b + " " +
	    c + " " +
	    s + " " +
	    i + " " +
	    l + " " +
	    f + " " +
	    Acme.Fmt.fmt( d ) + " " +
	    arrayToString( al ) + "]";
	}
    
    private static String arrayToString( long[] al )
	{
	if ( al == null )
	    return "null";
	StringBuffer sb = new StringBuffer();
	sb.append( "{" );
	for ( int i = 0; i < al.length; ++i )
	    {
	    if ( i != 0 )
		sb.append( ", " );
	    sb.append( al[i] );
	    }
	sb.append( "}" );
	return sb.toString();
	}

    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -