InputStream transferTo(OutputStream) in Java

transferTo(OutputStream): This method is available in the java.io.InputStream class of Java.

Syntax:

long java.io.InputStream.transferTo(OutputStream out) throws IOException

This method takes one argument. This method reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read. In return, this input stream will be at end of the stream. This method does not close either stream.

This method may block indefinitely reading from the input stream or writing to the output stream. The behavior for the case where the input and/or output stream is asynchronously closed, or the thread interrupted during the transfer, is highly input and output stream specific, and therefore not specified.

Parameters: One parameter is required for this method.

out: the output stream, non-null.

Returns: the number of bytes transferred.

Throws:

1. IOException - if an I/O error occurs when reading or writing.

2. NullPointerException - if out is null.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputStreamtransferTo {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        OutputStream out = System.out;

        try {
            System.out.println(inputStream.transferTo(out));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStream.close();
    }
}

Output:

0


Approach 2: IOException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputStreamtransferTo {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        OutputStream out = System.out;

        inputStream.close();
        try {
            System.out.println(inputStream.transferTo(out));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

    }
}

Output:

IOException occurs


Approach 3: NullPointerException 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputStreamtransferTo {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        OutputStream out = null;

        try {
            System.out.println(inputStream.transferTo(out));
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStream.close();
    }
}

Output:

NullPointerException occurs


Some other methods of InputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when the end of the stream is detected.

close()This method closes this input stream and releases any system resources associated with the stream.

mark(int) This method marks the current position in this input stream.

markSupported()This method tests if this input stream supports the mark and reset methods.

InputStream.nullInputStream()This method returns a new InputStream that reads no bytes. The returned stream is initially open.

read()This method reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.

read(byte[])This method reads some number of bytes from the input stream and stores them into the buffer array b.

read(byte[], int, int)his method reads up to len bytes of data from the input stream into an array of bytes.

readAllBytes()This method reads all remaining bytes from the input stream.

readNBytes(int)This method reads up to a specified number of bytes from the input stream.

readNBytes(byte[], int, int)This method reads the requested number of bytes from the input stream into the given byte array.

reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.

skip(long) This method skips over and discards n bytes of data from this input stream.

skipNBytes(long)This method skips over and discards exactly n bytes of data from this input stream.

No comments:

Post a Comment