Java tutorial
//package com.java2s; /*! * This program is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software * Foundation. * * You should have received a copy of the GNU Lesser General Public License along with this * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html * or from the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * Copyright (c) 2002-2013 Pentaho Corporation.. All rights reserved. */ import java.io.IOException; import java.io.InputStream; public class Main { /** * WARNING: if the <param>inStream</param> instance does not support mark/reset, when this method returns, * subsequent reads on <param>inStream</param> will be 256 bytes into the stream. This may not be the expected * behavior. FileInputStreams are an example of an InputStream that does not support mark/reset. InputStreams * that do support mark/reset will be reset to the beginning of the stream when this method returns. * * @param inStream * @return * @throws IOException */ public static String readEncodingProcessingInstruction(final InputStream inStream) throws IOException { final int BUFF_SZ = 256; if (inStream.markSupported()) { inStream.mark(BUFF_SZ + 1); // BUFF_SZ+1 forces mark to NOT be forgotten } byte[] buf = new byte[BUFF_SZ]; int totalBytesRead = 0; int bytesRead; do { bytesRead = inStream.read(buf, totalBytesRead, BUFF_SZ - totalBytesRead); if (bytesRead == -1) { break; } totalBytesRead += bytesRead; } while (totalBytesRead < BUFF_SZ); if (inStream.markSupported()) { inStream.reset(); } return new String(buf); } }