0

I am trying to build a mapreduce code to read and write an image, but I am getting ArrayIndexOutOfBoundsException when using ImageIO.write to save the image, although, I am not making any changes to the image. Here is the code :

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import java.io.BufferedInputStream;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.Writable;


public class mrimage implements Writable {
private static final Log log = LogFactory.getLog(mrimage.class);


BufferedImage reader = null;
String FileName;


public mrimage(){

}

public mrimage(BufferedImage buff,String fileName){
    this.reader = buff;
    this.FileName = fileName;
}

public BufferedImage getImage(int i){
return this.reader;
}

//reading generated image files
@Override
public void readFields(DataInput in) throws IOException { 
    try{
        reader = ImageIO.read(new BufferedInputStream((InputStream)in));

    }
    catch(Exception e){
        log.info(e);
    }
}

//writing the image files 
@Override
public void write(DataOutput out) throws IOException {

    try{
          ImageIO.write(reader, "jpg", (OutputStream)out);

    }
    catch(Exception e){
    }
}
}

can anyone help please?

this is the full error message that I am getting:

java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.ByteArrayInputStream.read(ByteArrayInputStream.java:174)
at java.io.DataInputStream.read(DataInputStream.java:132)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at javax.imageio.stream.FileCacheImageInputStream.readUntil(FileCacheImageInputStream.java:121)
at javax.imageio.stream.FileCacheImageInputStream.read(FileCacheImageInputStream.java:167)
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:337)
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:347)
at com.sun.imageio.plugins.bmp.BMPImageReaderSpi.canDecodeInput(BMPImageReaderSpi.java:66)
at javax.imageio.ImageIO$CanDecodeInputFilter.filter(ImageIO.java:541)
at javax.imageio.spi.FilterIterator.advance(ServiceRegistry.java:793)
at javax.imageio.spi.FilterIterator.<init>(ServiceRegistry.java:787)
at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:491)
at javax.imageio.ImageIO.getImageReaders(ImageIO.java:620)
at javax.imageio.ImageIO.read(ImageIO.java:1412)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at imagetojpeg.KUImage.readFields(KUImage.java:33)
at org.apache.hadoop.io.serializer.WritableSerialization$WritableDeserializer.deserialize(WritableSerialization.java:67)
at org.apache.hadoop.io.serializer.WritableSerialization$WritableDeserializer.deserialize(WritableSerialization.java:40)
at org.apache.hadoop.mapreduce.ReduceContext.nextKeyValue(ReduceContext.java:116)
at org.apache.hadoop.mapreduce.ReduceContext.nextKey(ReduceContext.java:92)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:175)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:649)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:417)
at org.apache.hadoop.mapred.Child$4.run(Child.java:261)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.Child.main(Child.java:255)
  • 1
    Kindly also get us the stacktrace. – Amar Mar 05 '13 at 19:18
  • Amar is this error message helpful? – user1602717 Mar 06 '13 at 16:05
  • So from looking at the stacktrace it seems that the exception is thrown while reading and not writing as you have mentioned in your question! The point where you should look is `at imagetojpeg.KUImage.readFields(KUImage.java:33)` – Amar Mar 06 '13 at 18:25
  • Do you have any idea why it would give this error? since I am using the same command in another code and doesn't give any errors – user1602717 Mar 06 '13 at 20:33

1 Answers1

-1

I ran into this problem and it turned out to be a mismatch between how I'd declared the output file and the input file. In my previous step I'd defined the output file as being of type SequenceFileAsBinaryOutputFormat, but in the next step I declared the input to be of type SequenceFileInputFormat. Once I changed the input format to SequenceFileAsBinaryInputFormat the problem went away.

Jeff B
  • 8,572
  • 17
  • 61
  • 140