Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 10th, 2012  |  syntax: None  |  size: 2.52 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Appcelerator Titanium Mobile
  3.  * Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
  4.  * Licensed under the terms of the Apache Public License
  5.  * Please see the LICENSE included with this distribution for details.
  6.  */
  7.  
  8. package org.appcelerator.titanium.util;
  9.  
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14.  
  15. import org.appcelerator.titanium.proxy.BufferProxy;
  16.  
  17.  
  18. public class TiStreamHelper
  19. {
  20.         private static final String LCAT = "TiStreamHelper";
  21.         private static final boolean DBG = TiConfig.LOGD;
  22.  
  23.         public static final int DEFAULT_BUFFER_SIZE = 1024;
  24.  
  25.  
  26.         public static int read(InputStream inputStream, BufferProxy bufferProxy)
  27.         {
  28.                 return read(inputStream, bufferProxy, 0, bufferProxy.getBuffer().length);
  29.         }
  30.  
  31.         public static int read(InputStream inputStream, BufferProxy bufferProxy, int offset, int length)
  32.         {
  33.                 byte[] buffer = bufferProxy.getBuffer();
  34.                 int bytesRead = -1;
  35.  
  36.                 if((offset + length) > buffer.length)
  37.                 {
  38.                         length = buffer.length - offset;
  39.                 }
  40.  
  41.                 try {
  42.                         bytesRead = inputStream.read(buffer, offset, length);
  43.  
  44.                 } catch (IOException e) {
  45.                         e.printStackTrace();
  46.                 }
  47.  
  48.                 return bytesRead;
  49.         }
  50.  
  51.         public static int write(OutputStream outputStream, BufferProxy bufferProxy)
  52.         {
  53.                 return write(outputStream, bufferProxy, 0, bufferProxy.getBuffer().length);
  54.         }
  55.  
  56.         public static int write(OutputStream outputStream, BufferProxy bufferProxy, int offset, int length)
  57.         {
  58.                 byte[] buffer = bufferProxy.getBuffer();
  59.  
  60.                 if((offset + length) > buffer.length)
  61.                 {
  62.                         length = buffer.length - offset;
  63.                 }
  64.  
  65.                 try {
  66.                         outputStream.write(buffer, offset, length);
  67.                         outputStream.flush();
  68.  
  69.                 } catch (IOException e) {
  70.                         e.printStackTrace();
  71.                 }
  72.  
  73.                 return length;
  74.         }
  75.  
  76.         // TODO old stuff begins here - remove everything below this once stream
  77.         // module if finished
  78.         public static void pump(InputStream in, OutputStream out)
  79.         {
  80.                 pump(in, out, DEFAULT_BUFFER_SIZE);
  81.         }
  82.  
  83.         public static void pump(InputStream in, OutputStream out, int bufferSize)
  84.         {
  85.                 byte buffer[] = new byte[bufferSize];
  86.                 int count = 0;
  87.                 try {
  88.                         while((count = in.read(buffer)) != -1) {
  89.                                 if (out != null) {
  90.                                         out.write(buffer, 0, count);
  91.                                 }
  92.                         }
  93.                 } catch (IOException e) {
  94.                         Log.e(LCAT, "IOException pumping streams", e);
  95.                 }
  96.         }
  97.  
  98.         public static String toString(InputStream in)
  99.         {
  100.                 ByteArrayOutputStream out = new ByteArrayOutputStream();
  101.                 pump(in, out);
  102.                 return new String(out.toByteArray());
  103.         }
  104. }