Android Open Source - WebImageView I O Utils






From Project

Back to project page WebImageView.

License

The source code is released under:

MIT License

If you think the Android project WebImageView 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

package com.rsv.utils;
//from w w  w.j a va  2  s  .com
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import com.rsv.comp.IProgressListener;

public class IOUtils {
  private static final int DEFAULT_BUFFER_SIZE = 1024;

  public static void copyStream(InputStream is, OutputStream os) throws IOException {
    copyStream(is, os, 1024, null);
  }

  public static void copyStream(InputStream is, OutputStream os, int bufferSize,
      IProgressListener obs) throws IOException {
    final int buffer_size = bufferSize;

    byte[] bytes = new byte[buffer_size];

    long p = 0;

    while (true) {
      int count = is.read(bytes, 0, buffer_size);

      if (count == -1) {
        break;
      }

      os.write(bytes, 0, count);

      if (obs != null) {
        p += count;
        obs.reportProgress(p);
      }
    }
  }

  /**
   * http://www.iana.org/assignments/character-sets
   * 
   * @param input
   * @param encoding
   * @return
   * @throws IOException
   */
  public static String toString(InputStream input, String encoding) throws IOException {
    StringWriter sw = new StringWriter();
    copy(input, sw, encoding);
    return sw.toString();
  }

  public static void copy(InputStream input, Writer output, String encoding) throws IOException {
    if (encoding == null) {
      copy(input, output);
    } else {
      InputStreamReader in = new InputStreamReader(input, encoding);
      copy(in, output);
    }
  }

  public static void copy(InputStream input, Writer output) throws IOException {
    InputStreamReader in = new InputStreamReader(input);
    copy(in, output);
  }

  public static int copy(Reader input, Writer output) throws IOException {
    long count = copyLarge(input, output);
    if (count > Integer.MAX_VALUE) {
      return -1;
    }
    return (int) count;
  }

  public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
      output.write(buffer, 0, n);
      count += n;
    }
    return count;
  }
}




Java Source Code List

com.rsv.cache.BaseFileCache.java
com.rsv.cache.BaseMemCache.java
com.rsv.cache.LmtSizeMemCache.java
com.rsv.cache.LmtSpaceFileCache.java
com.rsv.comp.IProgressListener.java
com.rsv.comp.ImageLoader.java
com.rsv.config.ConfigReader.java
com.rsv.config.Constants.java
com.rsv.utils.FileUtils.java
com.rsv.utils.HttpClientUtils.java
com.rsv.utils.IOUtils.java
com.rsv.utils.ImgUtils.java
com.rsv.utils.LogUtils.java
com.rsv.utils.OSUtils.java
com.rsv.utils.StorageUtils.java
com.rsv.widget.WebImageView.java