Here you can find the source of loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish)
public static String loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish)
//package com.java2s; /****************************************************************************** * * Copyright 2014 Paphus Solutions Inc. * * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/legal/epl-v10.html * * 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./*from ww w . j ava 2s .c o m*/ * ******************************************************************************/ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; public class Main { /** * Get the contents of the stream to a .self file and parse it. */ public static String loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish) { if (encoding.trim().isEmpty()) { encoding = "UTF-8"; } // FEFF because this is the Unicode char represented by the UTF-8 byte order mark (EF BB BF). String UTF8_BOM = "\uFEFF"; StringWriter writer = new StringWriter(); InputStreamReader reader = null; try { reader = new InputStreamReader(stream, encoding); int size = 0; int next = reader.read(); boolean first = true; while (next >= 0) { if (first && next == UTF8_BOM.charAt(0)) { // skip } else { writer.write(next); } next = reader.read(); if (size > maxSize) { throw new RuntimeException( "File size limit exceeded: " + size + " > " + maxSize + " token: " + next); } size++; } } catch (IOException exception) { throw new RuntimeException("IO Error: " + exception.getMessage(), exception); } finally { if (reader != null && finish) { try { reader.close(); } catch (IOException ignore) { } } if (stream != null && finish) { try { stream.close(); } catch (IOException ignore) { } } } return writer.toString(); } }