Java Text File Read by Charset read(InputStream _is, CharsetDecoder _decoder)

Here you can find the source of read(InputStream _is, CharsetDecoder _decoder)

Description

read

License

Apache License

Parameter

Parameter Description
_is a parameter
_decoder a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String read(InputStream _is, CharsetDecoder _decoder) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  w w .j a  v a2s. co m
 * Copyright (c)2004-2011 Mark Logic Corporation
 *
 * 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.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.CharsetDecoder;

public class Main {
    protected static final int BUFFER_SIZE = 8 * 1024;

    /**
     * @param sb
     * @throws IOException
     */
    public static void read(Reader input, StringBuilder sb) throws IOException {
        // uses a reader, so charset translation should be ok
        int size;
        char[] buf = new char[BUFFER_SIZE];
        while ((size = input.read(buf)) > -1) {
            sb.append(buf, 0, size);
        }
        // mark for gc
        buf = null;
    }

    /**
     * @param _is
     * @param _decoder
     * @return
     * @throws IOException
     */
    public static String read(InputStream _is, CharsetDecoder _decoder) throws IOException {
        StringBuilder sb = new StringBuilder();
        read(new InputStreamReader(_is, _decoder), sb);
        return sb.toString();
    }

    /**
     * @param _is
     * @param _decoder
     * @return
     * @throws IOException
     */
    public static byte[] read(InputStream _is) throws IOException {

        if (null == _is) {
            throw new IOException("null InputStream");
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream(_is.available());
        byte[] buf = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = _is.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        // mark for gc
        buf = null;
        os.flush();
        return os.toByteArray();
    }
}

Related

  1. newReader(InputStream input, Charset encoding)
  2. newReader(ReadableByteChannel ch, Charset cs)
  3. newUTF8CharSetReader(String filename)
  4. openNewCompressedInputReader(final InputStream inputStream, final Charset charset)
  5. read(File file, Charset charset)
  6. read(InputStream in, Charset charset)
  7. read(InputStream in, Charset cs, Appendable appendable, int capacity)
  8. read(InputStream in, String charsetName)
  9. readAll(InputStream in, Charset charset)