Query on java I/O BufferedOutputStream write() method

Below is the code that is written using byte stream non-buffer class FileInputStream and FileOutputStream with the usage of explicit user buffers.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class FileCopyUserBuffer{
public static void main(String[] args){
String inFileStr = "C:\practice_in.jpg";
String outFileStr = "C:\practice_out.jpg";
FileInputStream in = null;
FileOutputStream out = null;
try{
in = new FileInputStream(inFileStr);
out = new FileOutputStream(outFileStr);
startTime = System.nanoTime();
byte[] byteBuf = new byte[4096];
int numBytesRead;
while((numBytesRead = in.read(byteBuf)) != -1){
out.write(byteBuf, 0, numBytesRead);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}
</code>
<code>public class FileCopyUserBuffer{ public static void main(String[] args){ String inFileStr = "C:\practice_in.jpg"; String outFileStr = "C:\practice_out.jpg"; FileInputStream in = null; FileOutputStream out = null; try{ in = new FileInputStream(inFileStr); out = new FileOutputStream(outFileStr); startTime = System.nanoTime(); byte[] byteBuf = new byte[4096]; int numBytesRead; while((numBytesRead = in.read(byteBuf)) != -1){ out.write(byteBuf, 0, numBytesRead); } }catch(IOException ex){ ex.printStackTrace(); } } } </code>
public class FileCopyUserBuffer{
    public static void main(String[] args){

        String inFileStr = "C:\practice_in.jpg";
        String outFileStr = "C:\practice_out.jpg";

        FileInputStream in = null;
        FileOutputStream out = null;

        try{
            in = new FileInputStream(inFileStr);
            out = new FileOutputStream(outFileStr);

            startTime = System.nanoTime();
            byte[] byteBuf = new byte[4096];
            int numBytesRead;

            while((numBytesRead = in.read(byteBuf)) != -1){
                out.write(byteBuf, 0, numBytesRead);
            }

        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}

Below is the program written using byte based buffer I/O stream class BufferedOutputStream & BufferedInputStream.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class FileCopyBufferedStream{
public void main(String[] args){
String inFileStr = "C:\practice_in.jpg";
String outFileStr = "C:\practice_out.jpg";
BufferedInputStream in = null;
BufferedOutputStream out = null;
File fileIn = new File(inFileStr);
System.out.println("File size is: " + fileIn.length() + " bytes");
try{
in = new BufferedInputStream(new FileInputStream(inFileStr));
out = new BufferedOutputStream(new FileOutputStream(outFileStr));
startTime = System.nanoTime();
int byteRead;
while((byteRead = in.read()) != -1){
out.write(byteRead);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}
</code>
<code>public class FileCopyBufferedStream{ public void main(String[] args){ String inFileStr = "C:\practice_in.jpg"; String outFileStr = "C:\practice_out.jpg"; BufferedInputStream in = null; BufferedOutputStream out = null; File fileIn = new File(inFileStr); System.out.println("File size is: " + fileIn.length() + " bytes"); try{ in = new BufferedInputStream(new FileInputStream(inFileStr)); out = new BufferedOutputStream(new FileOutputStream(outFileStr)); startTime = System.nanoTime(); int byteRead; while((byteRead = in.read()) != -1){ out.write(byteRead); } }catch(IOException ex){ ex.printStackTrace(); } } } </code>
public class FileCopyBufferedStream{
    public void main(String[] args){

        String inFileStr = "C:\practice_in.jpg";
        String outFileStr = "C:\practice_out.jpg";

        BufferedInputStream in = null;
        BufferedOutputStream out = null;


        File fileIn = new File(inFileStr);
        System.out.println("File size is: " + fileIn.length() + " bytes");

        try{
            in = new BufferedInputStream(new FileInputStream(inFileStr));
            out = new BufferedOutputStream(new FileOutputStream(outFileStr));

            startTime = System.nanoTime();
            int byteRead;
            while((byteRead = in.read()) != -1){
                out.write(byteRead);
            }

        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}

Above two programs perform the same functionality but am not clear about the functionality of write(#bytes) method of class BufferedOutputStream.

Based on the observation, below two classes have their own buffers.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class BufferedInputStream extends FilterInputStream {
.........
protected volatile byte buf[];
..........
}
public class BufferedOutputStream extends FilterOutputStream {
........
protected byte buf[];
.......
}
</code>
<code>public class BufferedInputStream extends FilterInputStream { ......... protected volatile byte buf[]; .......... } public class BufferedOutputStream extends FilterOutputStream { ........ protected byte buf[]; ....... } </code>
public class BufferedInputStream extends FilterInputStream {
    .........
    protected volatile byte buf[];
    ..........
}

public  class BufferedOutputStream extends FilterOutputStream {
    ........
    protected byte buf[];
    .......
}

In second program, How does out object of class BufferedOutputStream is able to write the buffer that is part of class BufferedInputStream? Because we are not passing buffer as argument of out.write(#bytes) method[which looks non-intuitive] unlike the first program which passes user buffer out.write(byteBuf,,)? In second program, out object should at-least have access to object reference in to access the buffer populated using in.read().

1

Because we are not passing buffer as argument of out.write(#bytes) method[which looks non-intuitive] unlike the first program which passes user buffer out.write(byteBuf,,)?

No, you are not passing buffer as argument,
you are passing the single byte that you have read.
You have that single byte in the byteRead variable,
returned by BufferedInputStream,
and you are passing that single byte into BufferedOutputStream.

In second program, out object should at-least have access to object reference in to access the buffer populated using in.read().

No, BufferedOutputStream doesn’t have access to BufferedInputStream‘s buffer.

It seems you simply misunderstood what .read() without arguments really does.
There’s nothing magical going on here.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật