List of usage examples for java.io InputStream available
public int available() throws IOException
From source file:com.ewcms.common.io.HtmlFileUtil.java
public static byte[] readByte(InputStream is) { try {//w w w .j av a 2s .c o m byte r[] = new byte[is.available()]; is.read(r); return r; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static int readNetSocketBytes(Socket socket, int timeoutMS, byte[] buffer, int maxLen) throws Exception { int ret = 0, timeout = -2, bytesAvailable = 0, curRead = 0, waitCount = 0; boolean haveData = false; InputStream is = socket.getInputStream(); while (timeout < timeoutMS) { bytesAvailable = is.available(); if (bytesAvailable > 0) { haveData = true;// w w w . ja va2 s. c om waitCount = 0; curRead = ((bytesAvailable > (maxLen - ret)) ? (maxLen - ret) : bytesAvailable); curRead = is.read(buffer, ret, curRead); ret += curRead; if (ret >= maxLen) { return ret; } } else { if (timeoutMS == -1) { break; } Thread.sleep(10); if (timeoutMS != 0) { timeout += 10; } else if ((haveData == true) && (waitCount++ > HAVE_DATA_WAIT_RETRIES)) { break; } } } return ret; }
From source file:Main.java
/** Gets Bitmap from inputStream downsampled. * * @param bis/*from ww w . jav a 2 s . c om*/ * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeSampledBitmapFromStream(InputStream bis, int reqWidth, int reqHeight) { InputStream is = new BufferedInputStream(bis); try { is.mark(is.available()); } catch (IOException e) { e.printStackTrace(); } // First decode with inJustDecodeBounds=true to check dimensions BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp0 = BitmapFactory.decodeStream(is, null, options); try { is.reset(); } catch (IOException e) { e.printStackTrace(); } // Calculate inSampleSize //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bmp = BitmapFactory.decodeStream(is, null, options); return bmp; }
From source file:Main.java
public static byte[] getAssetAsBuffer(Context ctx, String assetName) throws IOException { InputStream is = ctx.getAssets().open(assetName); int read, available, offset = 0; byte[] result = new byte[available = is.available()]; while (available > 0 && (read = is.read(result, offset, available)) != -1) { offset += read;/*from w w w. j ava 2 s . c o m*/ available = is.available(); if (offset + available > result.length) { byte[] newResult = new byte[offset + available]; System.arraycopy(result, 0, newResult, 0, offset); result = newResult; } } return result; }
From source file:fr.logfiletoes.Main.java
/** * Convert InputStream to String//from w ww. j av a 2s.c o m * @param is InputStream read to convert to String * @return String from InputStream * @throws IOException */ public static String inputSteamToString(InputStream is) throws IOException { byte[] buffer = new byte[1024]; StringBuilder sb = new StringBuilder(); while (is.available() > 0) { is.read(buffer); sb.append(new String(buffer)); } return sb.toString(); }
From source file:Main.java
public static String loadJSONFromResource(Context context, int resource) { if (resource <= 0 || context == null) return null; String json = null;// w ww . j av a 2s . co m InputStream is = context.getResources().openRawResource(resource); try { if (is != null) { int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); json = new String(buffer, "UTF-8"); } } catch (IOException e) { return null; } finally { try { if (is != null) is.close(); } catch (IOException e) { } } return json; }
From source file:Main.java
public static CharSequence toString(InputStream is) { try {// w w w . j a va 2 s . c om BufferedReader r = new BufferedReader(new InputStreamReader(is)); StringBuilder total = new StringBuilder(is.available()); String line; while ((line = r.readLine()) != null) { total.append(line); } return total; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:org.apache.sling.testing.clients.util.InputStreamBodyWithLength.java
/** * Returns the length of a resource (which is needed for the InputStreamBody * to work. Can't currently think of a better solution than going through * the resource stream and count.//from ww w. ja v a 2 s . co m * * @param resourcePath path to the file * @return the size of the resource */ private static long getResourceStreamLength(String resourcePath) throws ClientException { int streamLength = 0; InputStream stream = ResourceUtil.getResourceAsStream(resourcePath); try { for (int avail = stream.available(); avail > 0; avail = stream.available()) { streamLength += avail; stream.skip(avail); } } catch (IOException e) { throw new ClientException("Could not read " + resourcePath + "!", e); } finally { try { stream.close(); } catch (IOException e) { throw new ClientException("Could not close Inputstream for " + resourcePath + "!", e); } } return streamLength; }
From source file:Main.java
public static String getJsonDataFromAssets(Context context, String fileName) { StringBuilder stringBuilder = new StringBuilder(); InputStream inputStream = context.getClass().getClassLoader().getResourceAsStream("assets/" + fileName); try {// ww w . java 2s .c om byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); String json = new String(buffer, "utf-8"); stringBuilder = stringBuilder.append(json); } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return stringBuilder.toString(); }
From source file:Main.java
public static String decodeUTF8String(InputStream inputStream) { try {//from w ww . j av a2 s . c om if (inputStream == null) { return ""; } if (inputStream.available() <= 0) { return ""; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while ((len = inputStream.read(buff, 0, buff.length)) > 0) { baos.write(buff, 0, len); } String str = new String(baos.toByteArray(), "utf-8"); return str; } catch (Exception e) { // TODO: handle exception } return null; }