简体   繁体   中英

Rebuild byte array image

I'm trying to receive an image, coded in a byte array.

It works well if the image is not too big, but when I have to read many times the input stream to get all the image bytes, the line:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(finalData));

returns null .

Here is my code:

byte[]imgSize = new byte[SIZE_OF_LENGTH_ARRAY];
bis.read(imgSize, 0, SIZE_OF_LENGTH_ARRAY);
ByteBuffer bb = ByteBuffer.wrap(imgSize);
int size = bb.getInt();

System.out.println("Client: size="+size);
byte[] tmpData = new byte[size];
System.out.println("tmpData length = "+tmpData.length);
int readen = bis.read(tmpData, 0, tmpData.length);
System.out.println("readen="+readen);

byte[]finalData = new byte[size]; 

if(readen == size){
    finalData = tmpData;
}
else{
    int totalRead = readen;
    int j=1;

    while(totalRead<size){
        System.out.println("-----------append number "+j+"----------");
        System.out.println("totalRead="+totalRead);

        for(int i=0;i<tmpData.length;i++){
            finalData[i]=tmpData[i];
        }

        tmpData = new byte[size-totalRead];
        int tmpRead = bis.read(tmpData, 0, size-totalRead);
        System.out.println("tmpRead="+tmpRead);

        for(int i=0;i<tmpData.length;i++){
            finalData[i+totalRead]=tmpData[i];
        }

        totalRead+=tmpRead;
        j++;
    }
    System.out.println("totalRead final="+totalRead);
}

BufferedImage img = ImageIO.read(new ByteArrayInputStream(finalData));

And an output example is:

---Client: sending mess number 1---
Client: size=31099
tmpData length = 31099
readen=31099
---Client: sending mess number 2---
Client: size=85921
tmpData length = 85921
readen=17520
-----------append number 1----------
totalRead=17520
tmpRead=17520
-----------append number 2----------
totalRead=35040
tmpRead=17520
-----------append number 3----------
totalRead=52560
tmpRead=31408
-----------append number 4----------
totalRead=83968
tmpRead=1953
totalRead final=85921
image null

While I well read 85921 bytes, the ImageIO.Read makes a null image.

I don't know what bis is, but this line is probably wrong:

bis.read(imgSize, 0, SIZE_OF_LENGTH_ARRAY);

You should always check the returned value from a read. A read can read less than you specify.

Edit:

This is how you normally read all data from a stream:

byte[] buffer = new byte[1024 * 32];
int len = 0;
while ((len = in.read(buffer)) > -1) {
    out.write(buffer, 0, len);
}

Edit 2:

But why aren't you doing something like:

ImageIO.read(bis);

This line might be overwriting bytes from the previous while iteration:

 while(totalRead<size){
        System.out.println("-----------append number "+j+"----------");
        System.out.println("totalRead="+totalRead);
        //I suspect this is unnecessary and one source of the error
        //this loop would overwrite data from the last iteration's tmpData
        for(int i=0;i<tmpData.length;i++){ 
            finalData[i]=tmpData[i];
        }
        tmpData = new byte[size-totalRead];
        int tmpRead = bis.read(tmpData, 0, size-totalRead);
        System.out.println("tmpRead="+tmpRead);
        for(int i=0;i<tmpData.length;i++){
            finalData[i+totalRead]=tmpData[i];  //append tmpData
        }
        totalRead+=tmpRead;
        j++;
    }

Suppose you read 10 bytes in each while iteration.

In the first run you'd set all elements of final data to 0, then read 10 bytes and set elements 0-9 of finalData.

In the second iteration you now loop over the contents of tmpData (which is bytes 0-9) and overwrite bytes 0-9 to before reading bytes 10-19 and appending them. (so far so good)

In the third iteration you lop over the contents of tmpData (which is now bytes 10-19, from the second iteration) and overwrite bytes 0-9 in finalData. So here's one error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM