List of usage examples for java.io ByteArrayOutputStream reset
public synchronized void reset()
From source file:Main.java
public static byte[] compressBitmap(byte[] data, float size) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); if (bitmap == null || getSizeOfBitmap(bitmap) <= size) { return data; }//from w ww.j a v a2s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int quality = 100; while ((baos.toByteArray().length / 1024f) > size) { quality -= 5; baos.reset(); if (quality <= 0) { break; } bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); } byte[] byteData = baos.toByteArray(); return byteData; }
From source file:Main.java
public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; while (baos.toByteArray().length / 1024 > 100) { options -= 10;/*from w w w .j a va2s . com*/ if (options > 0) { baos.reset(); image.compress(Bitmap.CompressFormat.JPEG, options, baos); } } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); return BitmapFactory.decodeStream(isBm, null, null); }
From source file:Main.java
public static void compressFileIfNeeded(String filePath) { File f = new File(filePath); Bitmap bitmap;//from w ww .j ava 2s .c o m bitmap = BitmapFactory.decodeFile(filePath); int MAX_IMAGE_SIZE = 1000 * 1024; int streamLength = (int) f.length(); if (streamLength > MAX_IMAGE_SIZE) { int compressQuality = 105; ByteArrayOutputStream bmpStream = new ByteArrayOutputStream(); while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) { try { bmpStream.flush();//to avoid out of memory error bmpStream.reset(); } catch (IOException e) { e.printStackTrace(); } compressQuality -= 5; bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream); byte[] bmpPicByteArray = bmpStream.toByteArray(); streamLength = bmpPicByteArray.length; Log.d("test upload", "Quality: " + compressQuality); Log.d("test upload", "Size: " + streamLength); } FileOutputStream fo; try { f.delete(); f = new File(filePath); fo = new FileOutputStream(f); fo.write(bmpStream.toByteArray()); fo.flush(); fo.close(); ExifInterface exif = new ExifInterface(f.getAbsolutePath()); exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6"); exif.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
protected static byte[][] deriveKeyAndIV(byte[] salt, byte[] master_key) throws NoSuchAlgorithmException, IOException { byte[] key = new byte[KEY_LEN]; byte[] IV = new byte[IV_LEN]; MessageDigest md5 = MessageDigest.getInstance("MD5"); ByteArrayOutputStream data = new ByteArrayOutputStream(); ByteArrayOutputStream toHash = new ByteArrayOutputStream(); byte[] d = null; while (data.size() < IV_LEN + KEY_LEN) { md5.reset();//from w ww . j av a2 s . c om toHash.reset(); if (d != null) toHash.write(d); toHash.write(master_key); toHash.write(salt); d = md5.digest(toHash.toByteArray()); data.write(d); } byte[] output = data.toByteArray(); System.arraycopy(output, 0, key, 0, KEY_LEN); System.arraycopy(output, KEY_LEN, IV, 0, IV_LEN); return new byte[][] { key, IV }; }
From source file:Main.java
/** * @param maxSize unit kb/*from ww w.j a va 2s. co m*/ */ public static boolean compressSizeToFile(Bitmap bitmap, int maxSize, File outFile) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); while (baos.toByteArray().length / 1024.0 > maxSize) { if (quality < 30) { break; } quality -= 20; baos.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); } try { FileOutputStream fileOutputStream = new FileOutputStream(outFile); fileOutputStream.write(baos.toByteArray()); fileOutputStream.close(); } catch (Exception e) { return false; } return true; }
From source file:com.github.jinahya.codec.BossVsEngineerTestNanoTimeDecode.java
@BeforeClass private static void warmUp() throws IOException { LOGGER.info("warmUp()"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final long freeMemory = Runtime.getRuntime().freeMemory(); LOGGER.log(Level.INFO, "Runtime.freeMemory: {0}", freeMemory); for (int i = 0; i < 16; i++) { baos.reset(); while (baos.size() < 1048576L) { baos.write(Tests.decodedBytes()); }/* w ww .j a v a 2 s. c o m*/ final byte[] decoded = baos.toByteArray(); new HexEncoder().encodeLikeABoss(decoded); new HexEncoder().encodeLikeAnEngineer(decoded); } for (int i = 0; i < 16; i++) { baos.reset(); while (baos.size() < 1048576L) { baos.write(Tests.encodedBytes()); } final byte[] encoded = baos.toByteArray(); new HexDecoder().decodeLikeABoss(encoded); new HexDecoder().decodeLikeAnEngineer(encoded); } for (int i = 0; i < 128; i++) { decodeLikeABoss(Tests.encodedBytes()); decodeLikeAnEngineer(Tests.encodedBytes()); } }
From source file:com.github.jinahya.codec.BossVsEngineerTestNanoTimeEncode.java
@BeforeClass private static void warmUp() throws IOException { LOGGER.info("warmUp()"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final long freeMemory = Runtime.getRuntime().freeMemory(); LOGGER.log(Level.INFO, "Runtime.freeMemory: {0}", freeMemory); for (int i = 0; i < 16; i++) { baos.reset(); while (baos.size() < 1048576L) { baos.write(Tests.decodedBytes()); }/*from ww w . j a v a2s . c om*/ final byte[] decoded = baos.toByteArray(); new HexEncoder().encodeLikeABoss(decoded); new HexEncoder().encodeLikeAnEngineer(decoded); } for (int i = 0; i < 16; i++) { baos.reset(); while (baos.size() < 1048576L) { baos.write(Tests.encodedBytes()); } final byte[] encoded = baos.toByteArray(); new HexDecoder().decodeLikeABoss(encoded); new HexDecoder().decodeLikeAnEngineer(encoded); } for (int i = 0; i < 128; i++) { encodeLikeABoss(Tests.decodedBytes()); encodeLikeAnEngineer(Tests.decodedBytes()); } }
From source file:org.apache.hadoop.hbase.util.EncryptionTest.java
/** * Check that the specified cipher can be loaded and initialized, or throw * an exception. Verifies key and cipher provider configuration as a * prerequisite for cipher verification. * * @param conf/*ww w .ja v a 2s. c om*/ * @param cipher * @param key * @throws IOException */ public static void testEncryption(final Configuration conf, final String cipher, byte[] key) throws IOException { if (cipher == null) { return; } testKeyProvider(conf); testCipherProvider(conf); Boolean result = cipherResults.get(cipher); if (result == null) { try { Encryption.Context context = Encryption.newContext(conf); context.setCipher(Encryption.getCipher(conf, cipher)); if (key == null) { // Make a random key since one was not provided context.setKey(context.getCipher().getRandomKey()); } else { // This will be a wrapped key from schema context.setKey(EncryptionUtil.unwrapKey(conf, conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase"), key)); } byte[] iv = null; if (context.getCipher().getIvLength() > 0) { iv = new byte[context.getCipher().getIvLength()]; Bytes.random(iv); } byte[] plaintext = new byte[1024]; Bytes.random(plaintext); ByteArrayOutputStream out = new ByteArrayOutputStream(); Encryption.encrypt(out, new ByteArrayInputStream(plaintext), context, iv); byte[] ciphertext = out.toByteArray(); out.reset(); Encryption.decrypt(out, new ByteArrayInputStream(ciphertext), plaintext.length, context, iv); byte[] test = out.toByteArray(); if (!Bytes.equals(plaintext, test)) { throw new IOException("Did not pass encrypt/decrypt test"); } cipherResults.put(cipher, true); } catch (Exception e) { cipherResults.put(cipher, false); throw new IOException("Cipher " + cipher + " failed test: " + e.getMessage(), e); } } else if (result.booleanValue() == false) { throw new IOException("Cipher " + cipher + " previously failed test"); } }
From source file:org.openstatic.util.JSONUtil.java
public static InputStream json2html(InputStream is) throws Exception { try {/*from www.ja va 2 s . co m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int inputByte; while ((inputByte = is.read()) > -1) { baos.write(inputByte); } is.close(); String jdata = "<html><body>" + json2html(new String(baos.toByteArray())) + "</body></html>"; baos.reset(); ByteArrayInputStream data = new ByteArrayInputStream(jdata.getBytes()); return data; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); return null; } }
From source file:de.alpharogroup.io.SerializedObjectExtensions.java
/** * The Method toByteArray() serialize an Object to byte array. * * @param <T>//from ww w . j a v a2 s .c om * the generic type of the given object * @param object * The Object to convert into a byte array. * @return The byte array from the Object. * @throws IOException * Signals that an I/O exception has occurred. */ public static <T> byte[] toByteArray(final T object) throws IOException { ByteArrayOutputStream byteArrayOutputStream = null; ObjectOutputStream objectOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(FileConstants.KILOBYTE); byteArrayOutputStream.reset(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); return byteArrayOutputStream.toByteArray(); } finally { StreamExtensions.closeOutputStream(byteArrayOutputStream); StreamExtensions.closeOutputStream(objectOutputStream); } }