Back to project page javocsoft-toolbox.
The source code is released under:
GNU General Public License
If you think the Android project javocsoft-toolbox listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2010-2014 - JavocSoft - Javier Gonzalez Serrano * http://javocsoft.es/proyectos/code-libs/android/javocsoft-toolbox-android-library * /* ww w.j a v a2 s . co m*/ * This file is part of JavocSoft Android Toolbox library. * * JavocSoft Android Toolbox library is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * JavocSoft Android Toolbox library 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 General * Public License for more details. * * You should have received a copy of the GNU General Public License * along with JavocSoft Android Toolbox library. If not, see * <http://www.gnu.org/licenses/>. * */ package es.javocsoft.android.lib.toolbox.io; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; /** * Some useful IO operations. * * @author JavocSoft Team 2013 * @version 1.0 */ public class IOUtils { public static String convertStreamToString(InputStream is) throws IOException { /* * To convert the InputStream to String we use the * BufferedReader.readLine() method. We iterate until the BufferedReader * return null which means there's no more data to read. Each line will * appended to a StringBuilder and returned as String. */ if (is != null) { StringBuilder sb = new StringBuilder(); String line; try { BufferedReader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } finally { is.close(); } return sb.toString(); } else { return ""; } } public static ByteArrayInputStream inputStreamToByteArrayInputStream(InputStream is) throws IOException { byte[] buff = new byte[8000]; int bytesRead = 0; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while ((bytesRead = is.read(buff)) != -1) { bao.write(buff, 0, bytesRead); } byte[] data = bao.toByteArray(); return new ByteArrayInputStream(data); } /** * Compare two input stream * * @param input1 * the first stream * @param input2 * the second stream * @return true if the streams contain the same content, or false otherwise * @throws java.io.IOException * @throws IllegalArgumentException * if the stream is null */ public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException { boolean error = false; try { byte[] buffer1 = new byte[1024]; byte[] buffer2 = new byte[1024]; try { int numRead1 = 0; int numRead2 = 0; while (true) { numRead1 = input1.read(buffer1); numRead2 = input2.read(buffer2); if (numRead1 > -1) { if (numRead2 != numRead1) return false; // Otherwise same number of bytes read if (!Arrays.equals(buffer1, buffer2)) return false; // Otherwise same bytes read, so continue ... } else { // Nothing more in stream 1 ... return numRead2 < 0; } } } finally { input1.close(); } } catch (IOException e) { error = true; // this error should be thrown, even if there is an // error closing stream 2 throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an // error closing stream 2 throw e; } finally { try { input2.close(); } catch (IOException e) { if (!error) throw e; } } } /** * Read and return the entire contents of the supplied {@link java.io.InputStream * stream}. This method always closes the stream when finished reading. * * @param stream * the stream to the contents; may be null * @return the contents, or an empty byte array if the supplied reader is * null * @throws java.io.IOException * if there is an error reading the content */ public static byte[] readBytes(InputStream stream) throws IOException { if (stream == null) return new byte[] {}; byte[] buffer = new byte[1024]; ByteArrayOutputStream output = new ByteArrayOutputStream(); boolean error = false; try { int numRead = 0; while ((numRead = stream.read(buffer)) > -1) { output.write(buffer, 0, numRead); } } catch (IOException e) { error = true; // this error should be thrown, even if there is an // error closing stream throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an // error closing stream throw e; } finally { try { stream.close(); } catch (IOException e) { if (!error) throw e; } } output.flush(); return output.toByteArray(); } /** * Read and return the entire contents of the supplied {@link java.io.InputStream}. * This method always closes the stream when finished reading. * * @param stream * the streamed contents; may be null * @return the contents, or an empty string if the supplied stream is null * @throws java.io.IOException * if there is an error reading the content */ public static String read(InputStream stream) throws IOException { return stream == null ? "" : read(new InputStreamReader(stream)); } /** * Read and return the entire contents of the supplied {@link java.io.Reader}. This * method always closes the reader when finished reading. * * @param reader * the reader of the contents; may be null * @return the contents, or an empty string if the supplied reader is null * @throws java.io.IOException * if there is an error reading the content */ public static String read(Reader reader) throws IOException { if (reader == null) return ""; StringBuilder sb = new StringBuilder(); boolean error = false; try { int numRead = 0; char[] buffer = new char[1024]; while ((numRead = reader.read(buffer)) > -1) { sb.append(buffer, 0, numRead); } } catch (IOException e) { error = true; // this error should be thrown, even if there is an // error closing reader throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an // error closing reader throw e; } finally { try { reader.close(); } catch (IOException e) { if (!error) throw e; } } return sb.toString(); } }