Hi everyone,

Please bear with me as i have a rather silly question.
Lets say i have an object and i want to convert that object say a document or any other object to an array of bytes. Does anyone know how to do this?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

Here is a test class to get you started. I think this should help you.

import java.io.*;
import java.util.*;

class Bytes
{
	public static void main(String[] args)
	{
		try
		{
			[B]//create byte output stream[/B]
			ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
			
			[B]//creat object output stream and connect [/B] him to the byte output stream
			ObjectOutputStream out = new ObjectOutputStream(outBytes);
			
			[B]//create some object[/B]
			ArrayList list = new ArrayList();
			list.add("Hello");
			list.add("Moto");
			
			[B]//write the object to the object output stream[/B]
			out.writeObject(list);
			
			[B]//ask the byte output stream to get its bytes[/B]
			byte[] objAsBytes = outBytes.toByteArray();
			
			[B]//show the bytes (garbage though)[/B]
			System.out.println (new String(objAsBytes));
		}
		catch(IOException e)
		{
			e.printStackTrace();	
		}		
	}	
}

For more help, www.NeedProgrammingHelp.com

Hi everyone,

//write the object to the object output stream

out.writeObject(list);
			
//ask the byte output stream to get its bytes

byte[] objAsBytes = outBytes.toByteArray();

The above two lines i have some problems with.

You see if the list is extremely large then a out of memory exception is thrown

out.writeObject(list);

and as for the other line if the bytearray is too large the
an outofmemory is also thrown

byte[] objAsBytes = outBytes.toByteArray();

Basically what i am trying to do is trying to write out the object as bytes using a byte buffer of maybe 1K and never run out of memory something down the lines of this

FileOutputStream out = new FileOutputStream("C:/my_life_story.zip");
InputStream in = //I want to read the object from here

int len = 0;
byte[] buffer1 = new byte[1024]; 

while ((len = in.read(buffer1)) > 0)
{ 
out.write(buffer1, 0, len);
}
 
in.close(); 
out.close();

I am trying to do this so that when comes to memory i won't have to worry as i will be using the same buffer again

I hope someone can help me with this

Thank you

Yours Sincerely

Richard West
*****************************************************

Hi,

Have you tried increasing the JVM's Heap spaces? Research the '-Xms' & '-Xmx' command line parameters. Example:

Java -Xms65536k -Xmx65536k .....

Also '-XX:NewSize', '-XX:MaxNewSize', '-XX:PermSize' & '-XX:MaxPermSize' might be worth looking into. Example:

Java -XX:NewSize=30720k -XX:MaxNewSize=30720k -XX:PermSize=16384k -XX:MaxPermSize=16384k ...

Does your class implement the Serializable interface? Try the following link:

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

How big is this object? What does it contain?

Kate

Hi again,

I see this is also in another thread. I believe Jwenting has it right. I completly missed that you are not flushing the buffer, try:

while ((len = in.read(buffer1)) > 0) {
   out.write(buffer1, 0, len);
   out.flush();
}

Kate

Hi everyone,

You see i am trying to convert the object to a bytes of stream so i can serialize it.

You see if i use the below command line to serialize a large object(say 1G)
then a out of memory exception is thrown because it writes to the disk at one go and not by any buffer

ObjectOutputStream.writeObject(object);

Basically what i am trying to do is trying to write out the object as bytes using a byte buffer of maybe 1K and never run out of memory something down the lines of this

FileOutputStream out = new FileOutputStream("C:/my_life_story.zip");
InputStream in = //I want to read stream the object from here

int len = 0;
byte[] buffer1 = new byte[1024]; 

while ((len = in.read(buffer1)) > 0)
{ 
out.write(buffer1, 0, len);
}
 
in.close(); 
out.close();

I am trying to do this so that when comes to memory i won't have to worry as i will be using the same buffer again. This is something different from simply using a flush(). There has to be way i can get the OutputStream of an ObjectOuputStream

I hope someone can help me with this

Thank you

Yours Sincerely

Richard West

In your scenario you'd still have to read in the entire stream to create your object before you can start to serialize it.
Unless you can read in the input in small bits and flush your output before reading the next bit (or at least periodically) you're going to run out of memory at some point.

requesting garbage collection periodically can delay that a bit but it won't stop it (as garbage collection is guaranteed to run before the JVM runs out of memory, which is the only guarantee you're getting with the gc, but in your case as it stands now it'll do you little good as you're holding on to all that memory so it can't be reclaimed).

Hi,

I think something has been overlooked here. I am curious as to what a 1GB 'Object' looks like? What is the structure of this 'Object'?

How would you instantiate it? What kind of data-structures could hold such information in a single object? Wouldn't it be unmanagable to both you and more likely the system (JVM).

If your never going to instantiate this Object then you are simply copying raw data from one source (file?) to another. In either case you are going to want to flush the buffer.

Can you please walk us through exactly what your trying to achieve from the beginning with lots of detail, as I may be getting the wrong end of the stick here.

Kate

Hi everyone,

Can you please walk us through exactly what your trying to achieve from the beginning with lots of detail
Kate

You see the object is simply a document that when serialized to the disk has a size of 1G. The document consists of whatever is legal in it that means images, styled text, etc.

Everthing is alright except that when i read this file i get the out of memory exception because the readObject() method reads everthing at one go and not via a buffer.

I do instantiate the object and cast it to a document and set it to a JTextPane. But before i can do that i run out of memory
although there is more than enough memory. There has to be way to do this

Richard West

Hi everyone,

I do instantiate the object and cast it to a document and set it to a JTextPane. But before i can do that i run out of memory
although there is more than enough memory. There has to be way to do this

Richard West

Well, there may be enough physical memory but the JVM is probably not using it all. Normally the JVM does not allocate all available memory to the heap.:)

If you use JDK1.5, you could use jconsole (is in the bin directory of the jdk) to look at what the JVM is doing. DOCs at http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

I think there are similar tools for the 1.4 jdk also.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.