views:

54

answers:

2

Hi, I get a java.lang.ArrayIndexOutOfBoundsException when using ByteArrayInputStream.

First, I use a ZipInputStream to read through a zip file, and while looping through the zipEntries, I use a ByteArrayInputStream to capture the data of each zipEntry using the ZipInputStream.read(byte[] b) and ByteArrayInputStream(byte[] b) methods.

At the end, I have a total of 6 different ByteArrayInputStream objects containing data from 6 different zipEntries. I then use OpenCSV to read through each of the ByteArrayInputStream.

I have no problem reading 4 of the 6 ByteArrayInputStream objects, of which have byte sizes of less than 2000.

The other 2 ByteArrayInputStream objects have byte sizes of 2155 and 4010 respectively and the CSVreader was only able to read part of these 2 objects, then give an java.lang.ArrayIndexOutOfBoundsException.

This is the code I used to loop through the ZipInputStream

    InputStream fileStream = attachment.getInputStream();

    try {

        ZipInputStream zippy = new ZipInputStream(fileStream);
        ZipEntry entry = zippy.getNextEntry();

        ByteArrayInputStream courseData = null;

        while (entry!= null) {

            String name = entry.getName();
            long size = entry.getSize();

            if (name.equals("course.csv")) {
                courseData = copyInputStream(zippy, (int)size);
            }
            //similar IF statements for 5 other ByteArrayInputStream objects 
            entry = zippy.getNextEntry(); 
        }

        CourseDataManager.load(courseData);
    }catch(Exception e){
       e.printStackTrace();
    }

The following is the code with which I use to copy the data from the ZipInputStream to the ByteArrayInputStream.

public ByteArrayInputStream copyInputStream(InputStream in, int size)
        throws IOException {
    byte[] buffer = new byte[size];

    in.read(buffer);
    ByteArrayInputStream b = new ByteArrayInputStream(buffer);
    return b;

}

The 2 sets of openCSV codes are able to read a few lines of data, before throwing that exception, which leads me to believe that it is the byteArray that is causing the problem. Is there anything I can do or work around this problem? I am trying to make an application that accepts a zip file, while not storing any temporary files in the web app, as I am deploying to both google app engine and tomcat server.

Fixed!!! Thanks to stephen C, i realized that read(byte[]) does not read everything so I adjusted the code to make the copyInputStream fully functional.

A: 

On what line do you get the error? And have you checked the value of size? I suspect it's 0

Bart van Heukelom
hi, I get the error on the OpenCsv code which I use to read the byteArray. The sizes are not 0, they are as follows for the 6 different byteArrays: 125,655,90,1137,2155,4010. The last 2 byte arrays are the one giving the indexOutOfBoundsException. They are able to read part of the byteArray before giving that Exception, which leads me to believe that it is not an error in the OpenCSV code, but an error in the ByteArrayInputStream
JavaLava
+1  A: 

Since this looks like homework, here's a hint:

The read(byte[]) method returns the number bytes read.

Stephen C
ahhhh you are right. the 2 byte arrays that I am having problem with are not reading the full inputstream it seems. will work on solving that
JavaLava
omggg fixed it !!! thanks!!!!
JavaLava