InputStream to byte array, copy Reader and Writer, : Stream « File « Android






InputStream to byte array, copy Reader and Writer,

   
//package net.javalib.flickr.search.util;

import java.io.ByteArrayOutputStream;
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;

/**
 * IO Utilities.
 * Based on code from commons-io
 */
class IOUtil {
  public static byte[] toByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int read = 0;
    byte[] buffer = new byte[1024];
    while (read != -1) {
      read = in.read(buffer);
      if (read != -1)
        out.write(buffer,0,read);
    }
    out.close();
    return out.toByteArray();
  }
  
  public static String toString(InputStream in) throws IOException {
    StringWriter out = new StringWriter();
    copy(new InputStreamReader(in),out);
    out.close();
    in.close();
    return out.toString();
  }
  
  public static int copy(InputStream in, OutputStream out) throws IOException {
    int count = 0;
    int read = 0;
    byte[] buffer = new byte[1024];
    while (read != -1) {
      read = in.read(buffer);
      if (read != -1) {
        count += read;
        out.write(buffer,0,read);
      }
    }
    return count;
  }
  
  public static int copy(Reader input,Writer output) throws IOException {
        char[] buffer = new char[1024];
        int count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

}

   
    
    
  








Related examples in the same category

1.Playing Back Audio Streams
2.Stream Proxy
3.Copy Stream
4.Ini File Stream Reader
5.Read InputStream fully to a string
6.Read Stream to byte array
7.read String From Stream
8.Read stream Fully
9.Read InputStream with ByteArrayOutputStream
10.get Resource As Stream, Loads the resource from classpath
11.InputStream that notifies listeners of its progress.
12.String to InputStream
13.Context.getFileStreamPath
14.stream To String
15.stream To Bytes
16.Load/save Int Map Data
17.Write InputStream into a StringBuilder