Java BufferedOutputStream Create toBufferedStream(InputStream is)

Here you can find the source of toBufferedStream(InputStream is)

Description

Returns the given InputStream cast to a BufferedInputStream if possible, otherwise wraps it into a new BufferedInputStream .

License

Open Source License

Parameter

Parameter Description
is an input stream to convert to a buffered input stream

Exception

Parameter Description
NullPointerException if the argument is null

Return

a buffered input stream

Declaration

public static BufferedInputStream toBufferedStream(InputStream is) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import static com.google.common.base.Preconditions.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**/*from w w w . j a  v  a2  s. c o  m*/
     * Returns the given {@link InputStream} cast to a {@link BufferedInputStream} if possible,
     * otherwise wraps it into a new {@link BufferedInputStream}.
     * 
     * @param is an input stream to convert to a buffered input stream
     * @return a buffered input stream
     * @throws NullPointerException if the argument is null
     */
    public static BufferedInputStream toBufferedStream(InputStream is) {
        checkNotNull(is, "input stream must not be null");

        if (is instanceof BufferedInputStream)
            return (BufferedInputStream) is;

        return new BufferedInputStream(is);
    }

    /**
     * Returns the given {@link OutputStream} cast to a {@link BufferedOutputStream} if possible,
     * otherwise wraps it into a new {@link BufferedOutputStream}.
     * 
     * @param os an output stream to convert to a buffered output stream
     * @return a buffered output stream
     * @throws NullPointerException if the argument is null
     */
    public static BufferedOutputStream toBufferedStream(OutputStream os) {
        checkNotNull(os, "output stream must not be null");

        if (os instanceof BufferedOutputStream)
            return (BufferedOutputStream) os;

        return new BufferedOutputStream(os);
    }
}

Related

  1. toBuffered(OutputStream stream)
  2. toBufferedDataOutputStream(final OutputStream outputStream)
  3. toBufferedOutputStream(OutputStream os)