Java Text File Read by Charset readString(InputStream in, int numBytes, Charset encoding)

Here you can find the source of readString(InputStream in, int numBytes, Charset encoding)

Description

Read numBytes from an InputStream and encode them into a String.

License

Open Source License

Parameter

Parameter Description
in a parameter
numBytes a parameter
encoding a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String readString(InputStream in, int numBytes, Charset encoding) throws IOException 

Method Source Code


//package com.java2s;
// This package is part of the Spiralcraft project and is licensed under

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 65536;

    /**/*from  w  w w . j  av  a 2s. c  o m*/
     * Read numBytes from an InputStream and encode them into a String.
     * 
     * @param in
     * @param numBytes
     * @param encoding
     * @return
     * @throws IOException
     */
    public static String readString(InputStream in, int numBytes, Charset encoding) throws IOException {
        byte[] bytes = readBytes(in, numBytes);
        return new String(bytes, encoding);
    }

    public static byte[] readBytes(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copyRaw(in, out, DEFAULT_BUFFER_SIZE);
        return out.toByteArray();
    }

    public static byte[] readBytes(InputStream in, long len) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copyRaw(in, out, DEFAULT_BUFFER_SIZE, len);
        return out.toByteArray();
    }

    /**
     * Copy an InputStream to an OutputStream using a buffer of the specified size.
     */
    public static long copyRaw(InputStream in, OutputStream out, int bufferSize) throws IOException {
        byte[] buffer = new byte[bufferSize];
        long count = 0;
        for (;;) {
            int read = in.read(buffer, 0, buffer.length);
            if (read < 0) {
                break;
            }
            out.write(buffer, 0, read);
            count += read;
        }
        return count;

    }

    /**
     * Copy (len) bytes from an InputStream to an OutputStream 
     *   using a buffer of the specified size and blocking until the specified
     *   number of bytes is read.
     */
    public static long copyRaw(InputStream in, OutputStream out, int bufferSize, long len) throws IOException {
        if (bufferSize <= 0) {
            bufferSize = DEFAULT_BUFFER_SIZE;
        }
        byte[] buffer = new byte[bufferSize];
        long count = 0;

        if (len <= 0) {
            for (;;) {
                int read = in.read(buffer, 0, buffer.length);
                if (read < 0) {
                    break;
                }
                out.write(buffer, 0, read);
                count += read;
            }
        } else {
            for (;;) {
                int read = in.read(buffer, 0, (int) Math.min(buffer.length, len - count));
                if (read < 0) {
                    break;
                }
                out.write(buffer, 0, read);
                count += read;
                if (count == len) {
                    break;
                }
            }
        }
        return count;
    }
}

Related

  1. readStreamToString(InputStream stream, Charset encoding)
  2. readString(DataInput input, int length, Charset charset)
  3. readString(final InputStream in, final Charset charset)
  4. readString(final InputStream in, final Charset charset)
  5. readString(final InputStream input, final Charset charset)
  6. readString(InputStream in, String charset)
  7. readStringFromStream(InputStream stream, Charset charset)
  8. readTextFile(final String fileNamePath, final String charsetName)
  9. readTextStream(InputStream is, Charset charset)