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

📄 serialutils.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SerialUtils - utilities for serializable objects
//
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>.  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// Visit the ACME Labs Java page for up-to-date versions of this and other
// fine Java utilities: http://www.acme.com/java/

package Acme;

import java.io.*;

/// Utilities for serializable objects.
// <P>
// These static routines help you serialize and deserialize the primitive
// data types.  Your own serialization routines will just be sequences of
// calls to these.
// <P>
// This implementation of serialization is much simpler and less convenient
// to use than the official one that will be in JDK1.1; but it has the
// significant albeit temporary advantage that it works in current browsers.
// <P>
// One thing this version does not do is handle cyclic graphs of objects.
// It only handles tree-shaped graphs.  If you need to serialize more
// complicated structures, consider using an ID-based scheme - instead of
// having your objects contain actual references to other objects, have
// them contain IDs which can be translated into real references by an
// ID-manager class.  This scheme also has the advantage that you don't
// have to deserialize the entire graph all at once, you can do it piece
// by piece as needed, and you can even set up a least-recently-used
// flush policy in the ID-manager.
// <P>
// <A HREF="/resources/classes/Acme/SerialUtils.java">Fetch the software.</A><BR>
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
// <P>
// @see Serializable

public class SerialUtils
    {

    private static String tagNull = "Null";
    private static String tagString = "String";
    private static String tagArrayObject = "ArrayObject";
    private static String tagArrayByte = "ArrayByte";
    private static String tagArrayChar = "ArrayChar";
    private static String tagArrayShort = "ArrayShort";
    private static String tagArrayInt = "ArrayInt";
    private static String tagArrayLong = "ArrayLong";
    private static String tagArrayFloat = "ArrayFloat";
    private static String tagArrayDouble = "ArrayDouble";

    /// Utility routine to return a class identifier - name plus version.
    private static String getIdentifier( Class cl )
	{
	// We have to create a temporary instance just to call getVersion(),
	// because Java currently lacks a way to call static methods given
	// a Class.
	try
	    {
	    Object o = cl.newInstance();
	    if ( o instanceof Acme.Versioned )
		{
		Acme.Versioned v = (Acme.Versioned) o;
		return cl.getName() + " " + v.getVersion();
		}
	    }
	catch ( IllegalAccessException e ) { }
	catch ( InstantiationException e ) { }
	// Instantiation problem, or it's not versionable; just use class name.
	return cl.getName();
	}

    /// Utility routine to write a serialization header.
    private static void serializeHeaderString( String str, DataOutputStream dout ) throws IOException
	{
	dout.writeUTF( "[" + str );
	}

    /// Utility routine to write a serialization header.
    private static void serializeHeader( Class cl, DataOutputStream dout ) throws IOException
	{
	serializeHeaderString( getIdentifier( cl ), dout );
	}

    /// Utility routine to write a serialization trailer.
    private static void serializeTrailerString( String str, DataOutputStream dout ) throws IOException
	{
	// dout.writeUTF( str + "]" );
	dout.writeUTF( "]" );
	}

    /// Utility routine to write a serialization trailer.
    private static void serializeTrailer( Class cl, DataOutputStream dout ) throws IOException
	{
	serializeTrailerString( getIdentifier( cl ), dout );
	}

    /// Utility routine to serialize a sub-object.
    private static void serializeNull( DataOutputStream dout ) throws IOException
	{
	serializeHeaderString( tagNull, dout );
	serializeTrailerString( tagNull, dout );
	}

    /// Utility routine to serialize a sub-object.
    public static void serializeObject( Acme.Serializable ser, DataOutputStream dout ) throws IOException
	{
	if ( ser == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeader( ser.getClass(), dout );
	    ser.serialize( dout );
	    serializeTrailer( ser.getClass(), dout );
	    }
	}

    /// Utility routine to serialize a String.
    public static void serializeString( String str, DataOutputStream dout ) throws IOException
	{
	if ( str == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagString, dout );
	    dout.writeUTF( str );
	    serializeTrailerString( tagString, dout );
	    }
	}

    /// Utility routine to serialize a boolean.
    public static void serializeBoolean( boolean b, DataOutputStream dout ) throws IOException
	{
	dout.writeBoolean( b );
	}

    /// Utility routine to serialize a byte.
    public static void serializeByte( byte b, DataOutputStream dout ) throws IOException
	{
	dout.writeByte( b );
	}

    /// Utility routine to serialize a char.
    public static void serializeChar( char c, DataOutputStream dout ) throws IOException
	{
	dout.writeChar( c );
	}

    /// Utility routine to serialize a short.
    public static void serializeShort( short s, DataOutputStream dout ) throws IOException
	{
	dout.writeShort( s );
	}

    /// Utility routine to serialize a int.
    public static void serializeInt( int i, DataOutputStream dout ) throws IOException
	{
	dout.writeInt( i );
	}

    /// Utility routine to serialize a long.
    public static void serializeLong( long l, DataOutputStream dout ) throws IOException
	{
	dout.writeLong( l );
	}

    /// Utility routine to serialize a float.
    public static void serializeFloat( float f, DataOutputStream dout ) throws IOException
	{
	dout.writeFloat( f );
	}

    /// Utility routine to serialize a double.
    public static void serializeDouble( double d, DataOutputStream dout ) throws IOException
	{
	dout.writeDouble( d );
	}

    /// Utility routine to serialize an array of Objects.
    public static void serializeArrayObject( Serializable[] ao, DataOutputStream dout ) throws IOException
	{
	if ( ao == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayObject, dout );
	    dout.writeShort( ao.length );
	    for ( int i = 0; i < ao.length; ++i )
		serializeObject( ao[i], dout );
	    serializeTrailerString( tagArrayObject, dout );
	    }
	}

    /// Utility routine to serialize an array of bytes.
    public static void serializeArrayByte( byte[] ab, DataOutputStream dout ) throws IOException
	{
	if ( ab == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayByte, dout );
	    dout.writeShort( ab.length );
	    for ( int i = 0; i < ab.length; ++i )
		dout.writeByte( ab[i] );
	    serializeTrailerString( tagArrayByte, dout );
	    }
	}

    /// Utility routine to serialize an array of chars.
    public static void serializeArrayChar( char[] ac, DataOutputStream dout ) throws IOException
	{
	if ( ac == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayChar, dout );
	    dout.writeShort( ac.length );
	    for ( int i = 0; i < ac.length; ++i )
		dout.writeChar( ac[i] );
	    serializeTrailerString( tagArrayChar, dout );
	    }
	}

    /// Utility routine to serialize an array of shorts.
    public static void serializeArrayShort( short[] as, DataOutputStream dout ) throws IOException
	{
	if ( as == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayShort, dout );
	    dout.writeShort( as.length );
	    for ( int i = 0; i < as.length; ++i )
		dout.writeShort( as[i] );
	    serializeTrailerString( tagArrayShort, dout );
	    }
	}

    /// Utility routine to serialize an array of ints.
    public static void serializeArrayInt( int[] ai, DataOutputStream dout ) throws IOException
	{
	if ( ai == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayInt, dout );
	    dout.writeShort( ai.length );
	    for ( int i = 0; i < ai.length; ++i )
		dout.writeInt( ai[i] );
	    serializeTrailerString( tagArrayInt, dout );
	    }
	}

    /// Utility routine to serialize an array of longs.
    public static void serializeArrayLong( long[] al, DataOutputStream dout ) throws IOException
	{
	if ( al == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayLong, dout );
	    dout.writeShort( al.length );
	    for ( int i = 0; i < al.length; ++i )
		dout.writeLong( al[i] );
	    serializeTrailerString( tagArrayLong, dout );
	    }
	}

    /// Utility routine to serialize an array of floats.
    public static void serializeArrayFloat( float[] af, DataOutputStream dout ) throws IOException
	{
	if ( af == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayFloat, dout );
	    dout.writeShort( af.length );
	    for ( int i = 0; i < af.length; ++i )
		dout.writeFloat( af[i] );
	    serializeTrailerString( tagArrayFloat, dout );
	    }
	}

    /// Utility routine to serialize an array of doubles.
    public static void serializeArrayDouble( double[] ad, DataOutputStream dout ) throws IOException
	{
	if ( ad == null )
	    serializeNull( dout );
	else
	    {
	    serializeHeaderString( tagArrayDouble, dout );
	    dout.writeShort( ad.length );
	    for ( int i = 0; i < ad.length; ++i )
		dout.writeDouble( ad[i] );
	    serializeTrailerString( tagArrayDouble, dout );
	    }
	}


    /// Utility routine to read a serialization header.
    // Special case: returns false if the object is null.
    private static boolean deserializeHeaderString( String str, DataInputStream din ) throws IOException
	{
	String s2 = din.readUTF();
	if ( s2.equals( "[" + tagNull ) )
	    return false;
	if ( ! s2.equals( "[" + str ) )
	    throw new IOException( "bogus serialization header" );
	return true;
	}

    /// Utility routine to read a serialization header.
    // Special case: returns false if the object is null.
    public static boolean deserializeHeader( Class cl, DataInputStream din ) throws IOException
	{
	return deserializeHeaderString( getIdentifier( cl ), din );
	}

    /// Utility routine to read a serialization trailer.
    private static void deserializeTrailerString( String str, DataInputStream din ) throws IOException
	{
	String s2 = din.readUTF();
	// if ( ! s2.equals( str + "]" ) )
	//     throw new IOException( "bogus serialization trailer" );
	if ( ! s2.equals( "]" ) )
	    throw new IOException( "bogus serialization trailer" );
	}

    /// Utility routine to read a serialization trailer.
    public static void deserializeTrailer( Class cl, DataInputStream din ) throws IOException
	{
	deserializeTrailerString( getIdentifier( cl ), din );
	}

    /// Utility routine to deserialize a sub-object.
    public static Serializable deserializeObject( Class cl, DataInputStream din ) throws IOException
	{
	Serializable o;
	if ( deserializeHeader( cl, din ) )
	    {
	    try
		{
		o = (Serializable) cl.newInstance();
		}
	    catch ( IllegalAccessException e )
		{
		throw new IOException( e.toString() );
		}
	    catch ( InstantiationException e )
		{
		throw new IOException( e.toString() );
		}

⌨️ 快捷键说明

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