Java InputStream to Writer inputStreamToWriter(final InputStream in, final Writer out)

Here you can find the source of inputStreamToWriter(final InputStream in, final Writer out)

Description

Copies the content of an InputStream to a Writer.

License

Open Source License

Parameter

Parameter Description
in <code>InputStream</code> from which data is read.
out <code>Writer</code> to which data is written.

Exception

Parameter Description
IOException an exception

Return

number of bytes read/written.

Declaration

public static long inputStreamToWriter(final InputStream in, final Writer out) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from w w w. j  a v  a2 s.c  o m
 ******************************************************************************/

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * The JCL book specifies the default buffer size as 8K characters.
     */
    public static final int DEFAULT_BUFFER = 8192;

    /**
     * Copies the content of an <code>InputStream</code> to a
     * <code>Writer</code>. Will flush the <code>Writer</code>, but won't close
     * <code>InputStream</code> or <code>Writer</code>.
     *
     * @param in
     *            <code>InputStream</code> from which data is read.
     * @param out
     *            <code>Writer</code> to which data is written.
     * @return number of bytes read/written.
     * @throws IOException
     */
    public static long inputStreamToWriter(final InputStream in, final Writer out) throws IOException {
        final InputStreamReader inr = new InputStreamReader(in);
        return readerToWriter(inr, out);
    }

    /**
     * Copies the content of an <code>InputStream</code> to a
     * <code>Writer</code>. Will flush the <code>Writer</code>, but won't close
     * <code>InputStream</code> or <code>Writer</code>.
     *
     * @param in
     *            <code>InputStream</code> from which data is read.
     * @param out
     *            <code>Writer</code> to which data is written.
     * @param buffer
     *            the number of bytes to buffer reading.
     * @return number of bytes read/written.
     * @throws IOException
     */
    public static long inputStreamToWriter(final InputStream in, final Writer out, final int buffer)
            throws IOException {
        final InputStreamReader inr = new InputStreamReader(in);
        return readerToWriter(inr, out, buffer);
    }

    /**
     * Copies the content of a <code>Reader</code> to a <code>Writer</code>.
     * Will flush the <code>Writer</code>, but won't close <code>Reader</code>
     * or <code>Writer</code>.
     *
     * @param reader
     *            <code>Reader</code> from which data is read.
     * @param writer
     *            <code>Writer</code> to which data is written.
     * @return number of bytes read/ written.
     * @throws IOException
     */
    public static long readerToWriter(final Reader reader, final Writer writer) throws IOException {
        return readerToWriter(reader, writer, 0);
    }

    /**
     * Copies the content of a <code>Reader</code> to a <code>Writer</code>.
     * Will flush the <code>Writer</code>, but won't close <code>Reader</code>
     * or <code>Writer</code>.
     *
     * @param reader
     *            <code>Reader</code> from which data is read.
     * @param writer
     *            <code>Writer</code> to which data is written.
     * @param buffer
     *            the number of bytes to buffer reading.
     * @return number of bytes read/written.
     * @throws IOException
     */
    public static long readerToWriter(final Reader reader, final Writer writer, int buffer) throws IOException {
        if (buffer < 1)
            buffer = DEFAULT_BUFFER;
        final char[] charBuffer = new char[buffer];
        long count = 0;
        int n = 0;
        while ((n = reader.read(charBuffer)) != -1) {
            writer.write(charBuffer, 0, n);
            count += n;
        }
        writer.flush();
        return count;
    }
}

Related

  1. inputStreamToWriterToFile(InputStream in, File file)