Home > OS >  IO Image reading and writing: Is writing array of bytes different from writing byte at a time using
IO Image reading and writing: Is writing array of bytes different from writing byte at a time using

Time:09-11

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.

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.

CodePudding user response:

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).

CodePudding user response:

Baeldung.com provides information on stream.transferTo() method which does not incur an additional copy to Java heap

https://www.baeldung.com/java-inputstream-to-outputstream

Example code

@Test
public void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
    String initialString = "Hello World!";

    try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
         ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
        inputStream.transferTo(targetStream);

        assertEquals(initialString, new String(targetStream.toByteArray()));
    }
}

See how this transferTo is called with both streams as arguments

  • Related