Java - DataOutputStream size() method



Description

The Java DataOutputStream size() method returns the current value of the counter written, the number of bytes written to this data output stream so far. If the counter overflows, it will be wrapped to Integer.MAX_VALUE.

Declaration

Following is the declaration for java.io.DataOutputStream.size() method −

 public int size() 

Parameters

NA

Return Value

The method returns the value of the written field.

Exception

NA

Example - Usage of DataOutputStream size() method

The following example shows the usage of Java DataOutputStream size() method. We've created FileOutputStream and DataOutputStream reference. A byte[] buf is initialized with some byte values. A FileOutputStream object is created with a File. Then DataOutputStream is initialized with FileOutputStream object created before. Then byte array is iterated to write byte values to the dataoutputstream. During iteration, we're printing the size of the output stream. Finally we're closing all the streams.

DataOutputStreamDemo.java

 package com.tutorialspoint; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = null; DataOutputStream dos = null; byte[] buf = {87,64,72,31,90}; try { // create file output stream fos = new FileOutputStream("test.txt"); // create data output stream dos = new DataOutputStream(fos); int size = 0; // for each byte in the buffer for (byte b:buf) { dos.write(b); // current value of the counter written size = dos.size(); // print System.out.print("Size: "+size + "; "); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases all system resources from the streams if(fos!=null) fos.close(); if(dos!=null) dos.close(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Size: 1; Size: 2; Size: 3; Size: 4; Size: 5; 

Example - Usage of DataOutputStream size() method

The following example shows the usage of Java DataOutputStream size() method. We've created FileOutputStream and DataOutputStream reference. A byte[] buf is initialized with some byte values. A FileOutputStream object is created with a File. Then DataOutputStream is initialized with FileOutputStream object created before. Then byte array is iterated to write byte values to the dataoutputstream. Once all bytes are written, we're printing the size of the stream. Then as a special case, we're closing the stream and then check the size. It still works and prints the size of the stream. Finally we're closing all the streams.

DataOutputStreamDemo.java

 package com.tutorialspoint; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = null; DataOutputStream dos = null; byte[] buf = {87,64,72,31,90}; try { // create file output stream fos = new FileOutputStream("test.txt"); // create data output stream dos = new DataOutputStream(fos); int size = 0; // for each byte in the buffer for (byte b:buf) { dos.write(b); } // current value of the counter written size = dos.size(); // print the size System.out.print("Size: "+size + "; "); // close the stream. dos.close(); // print the size again. System.out.print("Size after closing: "+size + "; "); } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases all system resources from the streams if(fos!=null) fos.close(); if(dos!=null) dos.close(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Size: 5; Size after closing: 5; 

Example - Usage of DataOutputStream size() method

The following example shows the usage of Java DataOutputStream size() method.

DataOutputStreamDemo.java

 package com.tutorialspoint; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) { try { // Create a ByteArrayOutputStream to hold data in memory ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream(); // Wrap DataOutputStream around ByteArrayOutputStream DataOutputStream dataOutput = new DataOutputStream(byteArrayOutput); // Write various data types to the DataOutputStream dataOutput.writeInt(42); // 4 bytes dataOutput.writeDouble(3.14); // 8 bytes dataOutput.writeUTF("Hello, World!");// 2 bytes for length + 13 bytes for string // Use the size() method to get the number of bytes written int bytesWritten = dataOutput.size(); // Output the number of bytes written System.out.println("Total bytes written: " + bytesWritten); // Close the streams dataOutput.close(); byteArrayOutput.close(); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Total bytes written: 27 

Breakdown

  • Integer− 4 bytes

  • Double− 8 bytes

  • UTF String− 2 bytes (length) + 13 bytes ("Hello, World!") = 15 bytes.

Total− 4 + 8 + 15 = 27 bytes

Explanation

  • ByteArrayOutputStream− Acts as an in-memory buffer to hold the data.

  • DataOutputStream− Allows writing primitive data types in a portable way.

  • size() Method− Returns the total number of bytes written so far.

java_io_dataoutputstream.htm
Advertisements