Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IO Image reading and writing: Is writing array of bytes different from writing byte at a time using write(int b) method?

I am new to java IO and I tried to simply copy and paste a photo. I used two ways to achieve this the first works nicely but the second doesn’t.

This Code works fine.

try (BufferedInputStream input = new BufferedInputStream(new FileInputStream("photoOriginal.jpg"));
             BufferedOutputStream output =new BufferedOutputStream(new FileOutputStream("photoCopy.jpg"))) {
            try {
                int n =0;
                byte[] buf = new byte[4092];
                while((n = input.read(buf))!=-1){
                    output. Write(buf,0,n);
                    output.flush();
                }
} 
  } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
   }

But the second doesn’t work , after the program finished I find the copy File with the same exact size as the original but when trying to open it ,it shows format not supported error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

try (BufferedInputStream input = new BufferedInputStream(new FileInputStream("photoOriginal.jpg"));
             BufferedOutputStream output =new BufferedOutputStream(new FileOutputStream("photoCopy.jpg"))) {
            try {
                int byteRead = input.read();
                while (byteRead != -1) {
                    byteRead = input.read();
                    output.write(byteRead);
                    output.flush();
                }

                }
} 
  } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
   }

I don’t understand where the problem is, it seems that the 2 sample are doing the same thing.
Is reading to and writing from byte array different from reading and writing single byte at a time ?
Isn’t writing int to a Stream with write(int b) method only writes the lowest 8 bits and vice versa as said in Documentation ?

write
public abstract void write(int b)
throws IOException
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

hope someone will help.

>Solution :

You’re not writing out the first byte – you call input.read(), check that it’s not -1, but then call input.read() again:

// Broken code
int byteRead = input.read();
while (byteRead != -1) {
    byteRead = input.read();
    output.write(byteRead);
    output.flush();
}

If you just move the next input.read() call to the end of the loop, it will work:

// Working code with duplication
int byteRead = input.read();
while (byteRead != -1) {
    output.write(byteRead);
    output.flush();
    byteRead = input.read();
}

Or you could combine the "read and test" to avoid duplication:

// Working code without duplication
int byteRead;
while ((byteRead = input.read()) != -1) {
    output.write(byteRead);
    output.flush();
}

However, this is still a very inefficient way of copying a stream. Copying a chunk at a time, as per your first code, is much more efficient (or using the built-in transferTo method if you’re using Java 9 or higher, as rostamn79 notes).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading