Android Open Source - javocsoft-toolbox I O Utils






From Project

Back to project page javocsoft-toolbox.

License

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.

Java Source Code

/*
 * 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();
  }
}




Java Source Code List

es.javocsoft.android.lib.toolbox.ToolBox.java
es.javocsoft.android.lib.toolbox.ads.AdBase.java
es.javocsoft.android.lib.toolbox.ads.AdFragment.java
es.javocsoft.android.lib.toolbox.ads.AdInterstitial.java
es.javocsoft.android.lib.toolbox.ads.InterstitialAdsListener.java
es.javocsoft.android.lib.toolbox.analytics.CampaignInfo.java
es.javocsoft.android.lib.toolbox.analytics.CustomCampaignTrackingReceiver.java
es.javocsoft.android.lib.toolbox.encoding.Base64DecodingException.java
es.javocsoft.android.lib.toolbox.encoding.Base64.java
es.javocsoft.android.lib.toolbox.encoding.FileHelper.java
es.javocsoft.android.lib.toolbox.facebook.FacebookLoginFragment.java
es.javocsoft.android.lib.toolbox.facebook.FacebookShareFragment.java
es.javocsoft.android.lib.toolbox.facebook.FbTools.java
es.javocsoft.android.lib.toolbox.facebook.beans.AppRequestBean.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestDeleteSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedErrorActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLoginActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLogoutActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBException.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBSessionException.java
es.javocsoft.android.lib.toolbox.gcm.EnvironmentType.java
es.javocsoft.android.lib.toolbox.gcm.NotificationModule.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomGCMBroadcastReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomNotificationReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.GCMIntentService.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResponse.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResultItem.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMMessage.java
es.javocsoft.android.lib.toolbox.gcm.exception.GCMException.java
es.javocsoft.android.lib.toolbox.gcm.send.GCMHttpDelivery.java
es.javocsoft.android.lib.toolbox.io.IOUtils.java
es.javocsoft.android.lib.toolbox.io.Unzipper.java
es.javocsoft.android.lib.toolbox.media.MediaScannerNotifier.java
es.javocsoft.android.lib.toolbox.net.HttpOperations.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTInfoHelper.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTShortNumberInformation.java
es.javocsoft.android.lib.toolbox.sms.observer.SMSObserver.java