Results 1 to 4 of 4
  1. #1
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default Serialization - writing/reading to memory

    I have been successfully able to write/read objects into file through the process of serialization. Am now trying to do the same by writing/reading into memory. While writing the code runs without exception, but while reading i get the java.io.StreamCorruptedException. My Code Listing for the serialization and de-serialization process are as follows: ( The class
    SerializableClass is serializable)

    Listing: Serialization:
    Java Code:
     
    [B][SIZE=2][COLOR=#7f0055]package [/COLOR][/SIZE][/B][SIZE=2]com.test;[/SIZE]
     
    [B][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/B][SIZE=2]java.io.ByteArrayOutputStream;[/SIZE]
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/B][SIZE=2]java.io.ObjectOutputStream;[/SIZE][/LEFT]
     
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/B][SIZE=2] SerlMemory {[/SIZE][/LEFT]
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055] public [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]static [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/B][SIZE=2] main(String[] args) {[/SIZE]
    
    [SIZE=2]SerializableClass serializableClass = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][SIZE=2] SerializableClass();[/SIZE]
    [LEFT][SIZE=2]ByteArrayOutputStream bos = [/SIZE][B][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    [SIZE=2]ObjectOutputStream out = [/SIZE][B][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
    [/LEFT]
    
     
     
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
    
    [SIZE=2]     bos = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][SIZE=2] ByteArrayOutputStream();[/SIZE]
    [LEFT][SIZE=2]     out = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][SIZE=2] ObjectOutputStream(bos);[/SIZE]
    [SIZE=2]     out.writeObject(serializableClass);[/SIZE]
    [SIZE=2]     out.flush();[/SIZE]
    [SIZE=2]     out.close();[/SIZE]
    [SIZE=2]     System.[I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println(bos.toByteArray().[/SIZE][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][SIZE=2]);[/SIZE][/SIZE]
    [SIZE=2]     } [/SIZE][/LEFT]
    [/LEFT]
    
     
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/B][SIZE=2] (Exception e) {[/SIZE][/LEFT]
     
    [LEFT][SIZE=2]             e.printStackTrace();[/SIZE]
    
    [SIZE=2]     }[/SIZE]
    [LEFT][SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
    [/LEFT]
    The print statement System.out.println(bos.toByteArray().length) gives a value of 114 meaning that the object is written into memory.

    Listing: De-Serialization:

    Java Code:
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]package [/COLOR][/SIZE][/B][SIZE=2]com.test;[/SIZE][/LEFT]
     
    [B][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/B][SIZE=2]java.io.ByteArrayInputStream;[/SIZE]
     
    [LEFT][B][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/B][SIZE=2]java.io.ObjectInputStream;[/SIZE][/LEFT]
     
    [B][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/B][SIZE=2] DeSerlMemory {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055] public [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]static [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/B][SIZE=2] main(String[] args) {[/SIZE]
     
    
    [LEFT][SIZE=2]ByteArrayInputStream bis = [/SIZE][B][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    [LEFT][SIZE=2]ObjectInputStream in = [/SIZE][B][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
    [/LEFT]
    
     
     
     
     
    [LEFT][B][SIZE=2][COLOR=#7f0055] try[/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
    
    [B][SIZE=2][COLOR=#7f0055]     byte[/COLOR][/SIZE][/B][SIZE=2][] buf = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/B][SIZE=2][1024];[/SIZE]
    [LEFT][SIZE=2]   bis = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][SIZE=2] ByteArrayInputStream(buf);[/SIZE]
    [SIZE=2]   in = [/SIZE][B][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/B][SIZE=2] ObjectInputStream(bis); //Exception Here[/SIZE]
    [SIZE=2]   SerializableClass serl = (SerializableClass) in.readObject();[/SIZE]
    [SIZE=2]   in.close();[/SIZE]
    [SIZE=2]   System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println(serl.[/SIZE][SIZE=2][COLOR=#0000c0]str1[/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]   System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println(serl.[/SIZE][SIZE=2][COLOR=#0000c0]str2[/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]     } [/SIZE][B][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/B][SIZE=2] (Exception e) {[/SIZE]
    [SIZE=2]          e.printStackTrace();[/SIZE]
    [SIZE=2]     }[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
    [/LEFT]
    The de-serialization process throws the java.io.StreamCorruptedException. Tried to understand this exception but in vain. Any ideas ?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    You wrote to a byte[] that is created by ByteArrayOutputStream. You then create a new, empty byte[] and read from it. And you get... Nothing.

    Instead, use bos.toByteArray() to retrieve the array of bytes you just wrote. Create a new ByteArrayInputStream using that array.

    At least it's serializing...

  3. #3
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Ok, I will need to get the same byte array that I just wrote into. My new code which works:

    Listing: Serializaion/De-serialization

    Java Code:
    package com.t;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    
    
    public class SerlMemory {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		
    		//Serialization
    		SerializableClass serl = new SerializableClass();
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		ObjectOutputStream out = new ObjectOutputStream(bos);
    		out.writeObject(serl);
    		
    		//De-serialization
    		ByteArrayInputStream bis = new   ByteArrayInputStream(bos.toByteArray());
    		ObjectInputStream in = new ObjectInputStream(bis);
    		SerializableClass in_serl = (SerializableClass) in.readObject();
    		System.out.println(in_serl.getName());
    	}
    
    }
    But, doesn't this also means that I will need to serialize and de-serialize the object within the boundary of a single class. Because once the main method of the above class end, the object bos cease to exist.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Well, you need to a reference at least to the byte array. You could have a

    public byte[] serializeToMemory() {}

    method, hold a reference to the byte[], and then have a

    public void deserializeFromMemory(byte[] bytes) {}

    method in another class.

Similar Threads

  1. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM
  2. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 03:05 PM
  3. writing/reading a Structure to/from a file
    By Farzaneh in forum New To Java
    Replies: 1
    Last Post: 08-31-2008, 03:21 PM
  4. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •