Java Text File Read by Charset readTextStream(InputStream is, Charset charset)

Here you can find the source of readTextStream(InputStream is, Charset charset)

Description

read Text Stream

License

Open Source License

Declaration

public static String readTextStream(InputStream is, Charset charset) throws IOException 

Method Source Code

//package com.java2s;
/* (The MIT License)/*from  w  ww.  j av a 2  s .  c o  m*/
Copyright (c) 2006 Adam Bennett (cruxic@gmail.com)
     
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
     
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
     
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */

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

public class Main {
    public static String readTextStream(InputStream is, Charset charset) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 4);
        redirectInput(is, baos, new byte[1024 * 2], -1);
        byte[] bytes = baos.toByteArray();
        baos = null;
        return new String(bytes, charset);
    }

    /**
     @param flushEveryNBytes flush after writing at least this many bytes to the output stream.  pass -1 to never flush.  pass 1 to flush with every non empty read.
     */
    public static int redirectInput(InputStream from, OutputStream to, byte[] readBuffer, int flushEveryNBytes)
            throws IOException {
        int nRead = 0;
        int totalRead = 0;
        int unflushed = 0;
        while (nRead != -1) {
            if (nRead > 0) {
                to.write(readBuffer, 0, nRead);
                totalRead += nRead;
                unflushed += nRead;
                if (unflushed >= flushEveryNBytes) {
                    unflushed = 0;
                    to.flush();
                }
            }

            nRead = from.read(readBuffer);
        }

        return totalRead;
    }
}

Related

  1. readString(final InputStream input, final Charset charset)
  2. readString(InputStream in, int numBytes, Charset encoding)
  3. readString(InputStream in, String charset)
  4. readStringFromStream(InputStream stream, Charset charset)
  5. readTextFile(final String fileNamePath, final String charsetName)
  6. readToBuffer(StringBuffer buffer, String filePath, Charset charset)
  7. readToString(InputStream in, Charset charset)
  8. readToString(ReadableByteChannel in, Charset charset)
  9. readURL(URL url, Charset encoding)