Java InputStream to Char Array getInputStreamAsCharArray(InputStream stream, int length, String encoding)

Here you can find the source of getInputStreamAsCharArray(InputStream stream, int length, String encoding)

Description

Returns the given input stream's contents as a character array.

License

Open Source License

Exception

Parameter Description
IOException if a problem occured reading the stream.

Declaration

public static char[] getInputStreamAsCharArray(InputStream stream,
        int length, String encoding) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from  w ww .j  av a 2  s  .com
 * Contributors:
 *     Rational Software - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Anton Leherbauer (Wind River Systems)
 *     IBM Corporation - EFS support
 *     Marc-Andre Laperle (Ericsson)
 *******************************************************************************/

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

public class Main {
    /**
     * Returns the given input stream's contents as a character array. If a
     * length is specified (ie. if length != -1), only length chars are
     * returned. Otherwise all chars in the stream are returned. Closes the stream.
     * 
     * @throws IOException
     *             if a problem occured reading the stream.
     */
    public static char[] getInputStreamAsCharArray(InputStream stream,
            int length, String encoding) throws IOException {
        final InputStreamReader reader = encoding == null ? new InputStreamReader(
                stream) : new InputStreamReader(stream, encoding);
        try {
            char[] contents;
            if (length == -1) {
                contents = new char[0];
                int contentsLength = 0;
                int charsRead = -1;
                do {
                    int available = stream.available();
                    // resize contents if needed
                    if (contentsLength + available > contents.length) {
                        System.arraycopy(contents, 0,
                                contents = new char[contentsLength
                                        + available], 0, contentsLength);
                    }
                    // read as many chars as possible
                    charsRead = reader.read(contents, contentsLength,
                            available);
                    if (charsRead > 0) {
                        // remember length of contents
                        contentsLength += charsRead;
                    }
                } while (charsRead > 0);
                // resize contents if necessary
                if (contentsLength < contents.length) {
                    System.arraycopy(contents, 0,
                            contents = new char[contentsLength], 0,
                            contentsLength);
                }
            } else {
                contents = new char[length];
                int len = 0;
                int readSize = 0;
                while ((readSize != -1) && (len != length)) {
                    // See PR 1FMS89U
                    // We record first the read size. In this case len is the
                    // actual read size.
                    len += readSize;
                    readSize = reader.read(contents, len, length - len);
                }
                // See PR 1FMS89U
                // Now we need to resize in case the default encoding used more
                // than one byte for each
                // character
                if (len != length)
                    System.arraycopy(contents, 0,
                            (contents = new char[len]), 0, len);
            }
            return contents;
        } finally {
            reader.close();
        }
    }
}

Related

  1. getInputStreamAsCharArray(InputStream stream, int length, String encoding)