How big is your test file?
How much memory does your JVM have ?
Print out Runtime.getRuntime().maxMemory() / (1024*1024) to get the Mb.
Since you are reading from a file, here is a simpler method to load the
contents of a file into a byte array:
/**
* Load a file into a byte array. <br>
* Only use this method for files small enough to fit into memory.
*
* @param file the file to load
* @return the byte array that is the contents of the file.
* @throws IOException
*/
public static byte[] load(File file) throws IOException
{
// Max file length of 2GB should not be a problem since will run out
of
// memory first.
int len = (int) file.length();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] data = new byte[len];
int off = 0;
int readLen = data.length;
do
{
int cnt = fileInputStream.read(data, off, readLen);
off += cnt;
readLen -= cnt;
} while (off < len);
fileInputStream.close();
return data;
}
On Fri, Jun 5, 2009 at 12:38 AM, Johnson nickel <[email protected]>wrote:
>
> Hi,
>
> I have problem with this readInputStream method. I have checked in
> (postgresql)database, which i
> used column data type is bytea its default column size 1gb data. I will
> explain my problem clearly,
>
>
> Step1 :
> I checked in java file itself,
>
> public static void main(String args[])
> {
> byte[] filedata = readInputStream(new FileInputStream("test.pdf"));
> // byte filedata = null
> // DB code to store binary data
>
> }
>
>
>
> If, i run the code, it shows outofmemory error java heap size.
>
>
>
>
>
>
> /** Read an input stream in its entirety into a byte array */
> public static byte[] readInputStream(InputStream inputStream) throws
> IOException {
> int bufSize = 1024 * 1024;
> byte[] content;
>
> List<byte[]> parts = new LinkedList();
> InputStream in = new BufferedInputStream(inputStream);
>
> byte[] readBuffer = new byte[bufSize];
> byte[] part = null;
> int bytesRead = 0;
>
> // read everyting into a list of byte arrays
> while ((bytesRead = in.read(readBuffer, 0, bufSize)) != -1) {
> part = new byte[bytesRead];
> System.arraycopy(readBuffer, 0, part, 0, bytesRead);
> parts.add(part);
> }
>
> // calculate the total size
> int totalSize = 0;
> for (byte[] partBuffer : parts) {
> totalSize += partBuffer.length;
> }
>
> // allocate the array
> content = new byte[totalSize];
> int offset = 0;
> for (byte[] partBuffer : parts) {
> System.arraycopy(partBuffer, 0, content, offset,
> partBuffer.length);
> offset += partBuffer.length;
> }
>
> return content;
> }
>
>
>
> --
> View this message in context:
> http://www.nabble.com/Huge-File-upload-in-struts-2-tp23870472p23881060.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>