List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public final static String getStringFormRaw(Context context, int rawId) { ByteArrayOutputStream baos = null; InputStream in = context.getResources().openRawResource(rawId); try {// w ww .j av a2 s. co m baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } baos.close(); return baos.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } finally { if (baos != null) { try { baos.close(); baos = null; } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java
public static String encrypt(String data, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); final Random r = new SecureRandom(); byte[] salt = new byte[SALT_SIZE]; r.nextBytes(salt);//from w w w.java 2s . co m SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC"); cipher.init(Cipher.ENCRYPT_MODE, fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE))); byte[] encVal = cipher.doFinal(data.getBytes()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // writing encrypted data along with the salt in the format readable by // open ssl api bos.write("Salted__".getBytes()); bos.write(salt); bos.write(encVal); String encryptedValue = new String(Base64.encode(bos.toByteArray())); bos.close(); return encryptedValue; }
From source file:Main.java
public static ByteArrayInputStream toByteStream(InputStream istream) { ByteArrayInputStream byteistream = new ByteArrayInputStream(new byte[0]); try {/*from w w w. j a va 2 s. co m*/ ByteArrayOutputStream byteostream = new ByteArrayOutputStream(8192); byte[] buffer = new byte[8192]; int lenght; while ((lenght = istream.read(buffer)) != -1) { byteostream.write(buffer, 0, lenght); } byteistream = new ByteArrayInputStream(byteostream.toByteArray()); byteostream.close(); } catch (Exception e) { } finally { try { istream.close(); } catch (Exception e) { } } return byteistream; }
From source file:com.amazonaws.client.util.sdk.IOUtils.java
/** * Reads and returns the rest of the given input stream as a byte array, * closing the input stream afterwards./*from ww w . java 2 s .c o m*/ */ public static byte[] toByteArray(InputStream is) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { byte[] b = new byte[BUFFER_SIZE]; int n = 0; while ((n = is.read(b)) != -1) { output.write(b, 0, n); } return output.toByteArray(); } finally { output.close(); } }
From source file:com.useekm.types.AbstractGeo.java
public static byte[] gzip(byte[] bytes) { try {/*w ww .ja v a 2 s. co m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos)); bufos.write(bytes); bufos.close(); byte[] retval = bos.toByteArray(); bos.close(); return retval; } catch (IOException e) { throw new IllegalStateException("Unexpected IOException on inmemory gzip", e); } }
From source file:Main.java
/** * File to Base64/*from w w w . j ava 2 s . co m*/ * @param filePath * @return * @throws FileNotFoundException * @throws IOException */ public static String getBase64StringFromFile(String filePath) throws FileNotFoundException, IOException { byte[] buffer = null; File file = new File(filePath); FileInputStream fis = null; ByteArrayOutputStream bos = null; byte[] b = new byte[1000]; try { fis = new FileInputStream(file); bos = new ByteArrayOutputStream(1000); int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return Base64.encodeToString(buffer, Base64.DEFAULT); }
From source file:info.bonjean.beluga.util.ResourcesUtil.java
private static byte[] getResourceAsByteArray(String resource) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream bais;//from ww w .ja v a 2 s .c o m try { bais = ResourcesUtil.class.getResourceAsStream(resource); int c; while ((c = bais.read()) != -1) { baos.write(c); } bais.close(); baos.close(); } catch (Exception e) { log.error("Cannot load resource " + resource); System.exit(-1); } return baos.toByteArray(); }
From source file:com.predic8.membrane.core.util.ByteUtil.java
public static byte[] getByteArrayData(InputStream stream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; while (true) { int read = stream.read(buffer); if (read < 0) break; bos.write(buffer, 0, read);/* w w w . j a v a 2 s . co m*/ } try { bos.close(); } catch (IOException e) { log.error("", e); } return bos.toByteArray(); }
From source file:com.moss.appsnap.keeper.HandyTool.java
private static void printStackTrace(Throwable t, StringBuilder text) { try {//from w w w . j a va2 s .co m ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream s = new PrintStream(out); t.printStackTrace(s); s.flush(); out.close(); text.append(new String(out.toByteArray())); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java
/** * Serialize a java Object into a byte array * // w w w. j a v a 2s. c o m * @param objToSerialize - Java Object to be serialized * @return a byte array serialized from the java Object * @throws IngestionException */ public static byte[] serializeObjToBinArr(Object objToSerialize) throws IngestionException { ByteArrayOutputStream binOut = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(binOut); out.writeObject(objToSerialize); out.close(); binOut.close(); } catch (IOException e) { throw new IngestionException(e); } return binOut.toByteArray(); }