List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation, final Rect croppingRect) throws IOException { byte[] rotated = rotateNV21(data, width, height, rotation); final int rotatedWidth = rotation % 180 > 0 ? height : width; final int rotatedHeight = rotation % 180 > 0 ? width : height; YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); previewImage.compressToJpeg(croppingRect, 80, outputStream); byte[] bytes = outputStream.toByteArray(); outputStream.close(); return bytes; }
From source file:Main.java
public static byte[] readInputStream(InputStream inStream) throws IOException { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0;// w ww . j a va 2 s.c o m while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); }
From source file:Main.java
/** * Converts image to byte array.//from ww w. ja v a2s. co m * <br /><br /> * <p/> * <b>ATTENTION</b>: image conversion is done by compression, witch is very long running operation. * * @param bmp original bitmap * @param format compress format * @param quality compress quality * @return compress image as byte array * @see android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) */ public static byte[] bmpToByte(Bitmap bmp, Bitmap.CompressFormat format, int quality) { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(format, quality, stream); try { stream.flush(); byte[] data = stream.toByteArray(); stream.close(); return data; } catch (IOException e) { return null; } }
From source file:Main.java
public static byte[] getFileByte(InputStream is) { try {/* w w w .j ava 2 s .c om*/ byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); is.close(); return data; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image;//from w w w . ja v a 2 s . co m int width; int height; try { image = ImageIO.read(new ByteArrayInputStream(rgb)); width = image.getWidth(); height = image.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color c = new Color(image.getRGB(j, i)); int red = (int) (c.getRed() * 0.299); int green = (int) (c.getGreen() * 0.587); int blue = (int) (c.getBlue() * 0.114); Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); image.setRGB(j, i, newColor.getRGB()); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] readStreamToBytes(InputStream is) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] byteArray = new byte[BYTE_ARRAY_SIZE]; int length = 0; while ((length = is.read(byteArray)) != -1) { bos.write(byteArray, 0, length); }/* w ww .j ava 2 s .co m*/ bos.close(); is.close(); return bos.toByteArray(); }
From source file:Main.java
public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation, final Rect croppingRect, final boolean flipHorizontal) throws IOException { byte[] rotated = rotateNV21(data, width, height, rotation, flipHorizontal); final int rotatedWidth = rotation % 180 > 0 ? height : width; final int rotatedHeight = rotation % 180 > 0 ? width : height; YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); previewImage.compressToJpeg(croppingRect, 80, outputStream); byte[] bytes = outputStream.toByteArray(); outputStream.close(); return bytes; }
From source file:com.sampullara.findmyiphone.FindMyDevice.java
private static void writeDeviceLocations(Writer out, String username, String password) throws IOException { // Initialize the HTTP client DefaultHttpClient hc = new DefaultHttpClient(); // Authorize with Apple's mobile me service HttpGet auth = new HttpGet( "https://auth.apple.com/authenticate?service=DockStatus&realm=primary-me&formID=loginForm&username=" + username + "&password=" + password + "&returnURL=aHR0cHM6Ly9zZWN1cmUubWUuY29tL3dvL1dlYk9iamVjdHMvRG9ja1N0YXR1cy53b2Evd2EvdHJhbXBvbGluZT9kZXN0aW5hdGlvblVybD0vYWNjb3VudA%3D%3D"); hc.execute(auth);/* ww w. j a v a 2 s. com*/ auth.abort(); // Pull the isc-secure.me.com cookie out so we can set the X-Mobileme-Isc header properly String isc = extractIscCode(hc); // Get access to the devices and find out their ids HttpPost devicemgmt = new HttpPost("https://secure.me.com/wo/WebObjects/DeviceMgmt.woa/?lang=en"); devicemgmt.addHeader("X-Mobileme-Version", "1.0"); devicemgmt.addHeader("X-Mobileme-Isc", isc); devicemgmt.setEntity(new UrlEncodedFormEntity(new ArrayList<NameValuePair>())); HttpResponse devicePage = hc.execute(devicemgmt); // Extract the device ids from their html encoded in json ByteArrayOutputStream os = new ByteArrayOutputStream(); devicePage.getEntity().writeTo(os); os.close(); Matcher m = Pattern.compile("DeviceMgmt.deviceIdMap\\['[0-9]+'\\] = '([a-z0-9]+)';") .matcher(new String(os.toByteArray())); List<String> deviceList = new ArrayList<String>(); while (m.find()) { deviceList.add(m.group(1)); } // For each available device, get the location JsonFactory jf = new JsonFactory(); JsonGenerator jg = jf.createJsonGenerator(out); jg.writeStartObject(); for (String device : deviceList) { HttpPost locate = new HttpPost( "https://secure.me.com/wo/WebObjects/DeviceMgmt.woa/wa/LocateAction/locateStatus"); locate.addHeader("X-Mobileme-Version", "1.0"); locate.addHeader("X-Mobileme-Isc", isc); locate.setEntity(new StringEntity( "postBody={\"deviceId\": \"" + device + "\", \"deviceOsVersion\": \"7A341\"}")); locate.setHeader("Content-type", "application/json"); HttpResponse location = hc.execute(locate); InputStream inputStream = location.getEntity().getContent(); JsonParser jp = jf.createJsonParser(inputStream); jp.nextToken(); // ugly jg.writeFieldName(device); jg.copyCurrentStructure(jp); inputStream.close(); } jg.close(); out.close(); }
From source file:Main.java
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle();/* w ww . ja v a 2 s . c o m*/ } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:StreamsUtils.java
public static byte[] readBytes(InputStream stream) throws IOException { ByteArrayOutputStream b = new ByteArrayOutputStream(); int readedBytes; byte[] buf = new byte[1024]; while ((readedBytes = stream.read(buf)) > 0) { b.write(buf, 0, readedBytes);/* ww w . ja va 2 s.c o m*/ } b.close(); return b.toByteArray(); }