Java InputStream Read All readAll(InputStream stream)

Here you can find the source of readAll(InputStream stream)

Description

Reads the entire content of a stream to a string.

License

LGPL

Parameter

Parameter Description
stream The stream to read.

Return

The string read from the stream or null if the stream was empty.

Declaration

public static String readAll(InputStream stream) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.*;
import java.util.Scanner;

public class Main {
    /**//  w  ww.  j  a  v a2  s  .  c  om
     * Reads the entire content of a stream to a string.
     *
     * @param stream The stream to read.
     * @return The string read from the stream or <code>null</code> if the stream was empty.
     */
    public static String readAll(InputStream stream) {
        Scanner scanner = new Scanner(stream).useDelimiter("\\A");
        return scanner.hasNext() ? scanner.next() : null;
    }
}

Related

  1. readAll(InputStream in, byte[] buffer, int off, int len)
  2. readall(InputStream in, byte[] buffer, int offset, int len)
  3. readAll(InputStream inputStream)
  4. readAll(InputStream is)
  5. readAll(InputStream is, byte[] buffer, int offset, int length)
  6. readAll(InputStream stream)
  7. readAllBuffer(InputStream stream, byte[] buffer)
  8. readAllFrom(java.io.InputStream is)
  9. readAllFromStream(InputStream is)