Java Text File Read by Charset readString(final InputStream in, final Charset charset)

Here you can find the source of readString(final InputStream in, final Charset charset)

Description

Reads all data from the given stream as character data in the given character set and closes it on completion or errors.

License

Apache License

Parameter

Parameter Description
in Input stream containing character data.
charset Character set of data in stream.

Exception

Parameter Description
IOException On IO errors.

Return

String of all data in stream.

Declaration

public static String readString(final InputStream in, final Charset charset) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w  w.  jav a2  s.  c o  m*/
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you 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 the following location:
 *
 *   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.
 */

import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    /** UTF-8 character set. */
    public static final Charset UTF8 = Charset.forName("UTF-8");

    /**
     * Reads all data from the given stream as UTF-8 character data and closes it on completion or errors.
     *
     * @param in Input stream containing character data.
     *
     * @return String of all data in stream.
     *
     * @throws IOException On IO errors.
     */
    public static String readString(final InputStream in) throws IOException {
        return readString(in, UTF8);
    }

    /**
     * Reads all data from the given stream as character data in the given character set and closes it on completion
     * or errors.
     *
     * @param in Input stream containing character data.
     * @param charset Character set of data in stream.
     *
     * @return String of all data in stream.
     *
     * @throws IOException On IO errors.
     */
    public static String readString(final InputStream in, final Charset charset) throws IOException {
        final Reader reader = new InputStreamReader(in, charset);
        final StringBuilder builder = new StringBuilder();
        final CharBuffer buffer = CharBuffer.allocate(2048);
        try {
            while (reader.read(buffer) > -1) {
                buffer.flip();
                builder.append(buffer);
            }
        } finally {
            closeQuietly(reader);
        }
        return builder.toString();
    }

    /**
     * Unconditionally close a {@link Closeable} resource. Errors on close are ignored.
     *
     * @param resource Resource to close.
     */
    public static void closeQuietly(final Closeable resource) {
        try {
            if (resource != null) {
                resource.close();
            }
        } catch (final IOException e) {
            //ignore
        }
    }
}

Related

  1. readLines(String path, Charset charset)
  2. readNextWord(BufferedInputStream in, Charset cs)
  3. readStreamAsString(final InputStream iStream, Charset iCharset)
  4. readStreamToString(InputStream stream, Charset encoding)
  5. readString(DataInput input, int length, Charset charset)
  6. readString(final InputStream in, final Charset charset)
  7. readString(final InputStream input, final Charset charset)
  8. readString(InputStream in, int numBytes, Charset encoding)
  9. readString(InputStream in, String charset)