Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.sapia.util.lang;
  
  import java.util.Date;

Author(s):
Jean-Cedric Desrochers
 
 public class SerializationUtils {

  
Utility method to read a UTF string from the object input stream.

Parameters:
in The object input stream from which to read the data.
Returns:
The created string object.
Throws:
java.io.IOException If an error occurs.
 
   public static String readUTFString(ObjectInput inthrows IOException {
     if (in.readBoolean()) {
       return in.readUTF();
     } else {
       return null;
     }
   }

  
Utility method to write a UTF string in the object output stream.

Parameters:
aValue The string value to encode.
out The object output stream to use.
Throws:
java.io.IOException If an erro occurs.
 
   public static void writeUTFString(String aValueObjectOutput outthrows IOException {
     if (aValue != null) {
       out.writeBoolean(true);
       out.writeUTF(aValue);
     } else {
       out.writeBoolean(false);
     }
   }
  
  
Utility method to write a java date in the object output stream.

Parameters:
aDate The date to encode.
out The object output stream to use.
Throws:
java.io.IOException If an erro occurs.
 
   public static void writeDate(Date aDateObjectOutput outthrows IOException {
     if (aDate == null) {
       out.writeByte(0);
     } else {
       if (aDate instanceof java.sql.Date) {
         out.writeByte(1);
       } else if (aDate instanceof java.sql.Time) {
         out.writeByte(2);
       } else if (aDate instanceof java.sql.Timestamp) {
         out.writeByte(3);
       } else {
         out.writeByte(4);
       }
       out.writeLong(aDate.getTime());
     }
   }
  
  
Utility method to read a java date from the object input stream.

Parameters:
in The object input stream from which to read the data.
Returns:
The created date object.
Throws:
java.io.IOException If an error occurs.
 
   public static Date readDate(ObjectInput inthrows IOException {
     byte flag = in.readByte();
     if (flag != 0) {
       long time = in.readLong();
       if (flag == 1) {
         return new java.sql.Date(time);
       } else if (flag == 2) {
         return new java.sql.Time(time);
       } else if (flag == 3) {
         return new java.sql.Timestamp(time);
       } else {
         return new Date(time);
       }
     } else {
       return null;
     }
   }
  
  
Utility method to read the content of a byte[] from the object output stream.

Parameters:
in The object input stream.
Returns:
The byte array with the data.
Throws:
java.io.IOException If an error occurs.
  public static byte[] readByteArray(ObjectInput inthrows IOException {
    byte[] someData = null;
    if (in.readBoolean()) {
      int size = in.readInt();
      someData = new byte[size];
      in.read(someData, 0, size);
    }
    return someData;
  }
  
  
Utility method that writes the content of the byte[] passed in to the object output stream.

Parameters:
someData The byte array to write.
out The object output stream.
Throws:
java.io.IOException If an error occurs.
  public static void writeByteArray(byte[] someDataObjectOutput outthrows IOException {
    if (someData == null) {
      out.writeBoolean(false);
    } else {
      out.writeBoolean(true);
      out.writeInt(someData.length);
      out.write(someData);
    }
  }
New to GrepCode? Check out our FAQ X