Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream write.

Prototype

public synchronized void write(int b) 

Source Link

Document

Writes the specified byte to this ByteArrayOutputStream .

Usage

From source file:com.tremolosecurity.provisioning.customTasks.CreateOTPKey.java

public static String generateEncryptedToken(String userID, GoogleAuthenticatorKey key, String hostName,
        ConfigManager cfg, String encryptionKey) throws ProvisioningException {
    TOTPKey totpkey = new TOTPKey();
    totpkey.setHost(hostName);//from   w  w  w. java 2  s  .  c  om
    totpkey.setScratchCodes(key.getScratchCodes());
    totpkey.setSecretKey(key.getKey());
    totpkey.setUserName(userID);
    totpkey.setValidationCode(key.getVerificationCode());

    Gson gson = new Gson();
    String json = gson.toJson(totpkey);
    SecretKey sc = cfg.getSecretKey(encryptionKey);
    String attrVal = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(json.getBytes("UTF-8"));

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, sc);

        byte[] encJson = cipher.doFinal(baos.toByteArray());
        String base64d = new String(org.bouncycastle.util.encoders.Base64.encode(encJson));

        Token token = new Token();
        token.setEncryptedRequest(base64d);
        token.setIv(new String(org.bouncycastle.util.encoders.Base64.encode(cipher.getIV())));

        json = gson.toJson(token);
        attrVal = new String(org.bouncycastle.util.encoders.Base64.encode(json.getBytes("UTF-8")));

    } catch (Exception e) {
        throw new ProvisioningException("Could not encrypt key", e);
    }
    return attrVal;
}

From source file:com.taobao.adfs.database.tdhsocket.client.protocol.TDHSProtocolBinary.java

private static void writeInt32ToStream(int v, ByteArrayOutputStream out) {
    for (int i = 3; i >= 0; i--) {
        out.write((byte) ((v >>> (8 * i)) & 0XFF));
    }//from w w  w  .  j  av  a  2s . c  om
}

From source file:edu.stanford.mobisocial.dungbeetle.model.Feed.java

public static int colorFor(String name) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/* w  ww .j a v a  2s  .  c o  m*/
        bos.write(name.getBytes());
    } catch (IOException e) {
    }
    SecureRandom r = new SecureRandom(bos.toByteArray());
    float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() };
    hsv[0] = hsv[0] + 20 * r.nextFloat() - 10;
    hsv[1] = hsv[1] * 0.2f + 0.8f;
    hsv[2] = hsv[2] * 0.2f + 0.8f;
    return Color.HSVToColor(hsv);
}

From source file:MainServer.UploadServlet.java

public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;/*from  w ww  .  ja v a 2 s . c om*/
    while ((i = is.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static String getChannelNum(Context context) {
    Resources resources = context.getResources();
    try {//from  ww  w. j  a va  2  s  . c o m
        Class<?> className = Class.forName(
                (new StringBuilder(String.valueOf(context.getPackageName()))).append(".R$raw").toString());
        Field field = className.getField("parent");
        Integer result = (Integer) field.get(className);
        InputStream num = resources.openRawResource(result);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = 1;
        try {
            while ((i = num.read()) != -1) {
                baos.write(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            resources = null;
            if (num != null) {
                try {
                    num.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new String(baos.toByteArray());
    } catch (Exception e) {
    }
    return "0";
}

From source file:com.taobao.tdhs.client.protocol.TDHSProtocolBinary.java

protected static void writeInt32ToStream(int v, ByteArrayOutputStream out) {
    for (int i = 3; i >= 0; i--) {
        out.write((byte) ((v >>> (8 * i)) & 0XFF));
    }/*from   w  w  w .  j  ava  2s.  c  o m*/
}

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {/*from w ww . j a v  a2s  . c  o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(fis);
        int ch;
        while ((ch = bis.read()) >= 0) {
            baos.write(ch);
        }
        baos.flush();
        return baos.toByteArray();
    } finally {
        fis.close();
    }
}

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);// w  w w.j  a  va2s  .c om

    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

private static String getStringFromRaw(Context context, int id) {
    String str;//from  ww w  .j  ava  2s . c  o  m
    try {
        Resources r = context.getResources();
        InputStream is = r.openRawResource(id);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            baos.write(i);
            i = is.read();
        }

        str = baos.toString();
        is.close();
    } catch (IOException e) {
        str = "";
    }

    return str;
}

From source file:Main.java

public static String readFromFileInput(Context context, String fileName) throws IOException {

    InputStream inputStream = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;//from w  ww . j  a  v a  2s  . c o  m
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
}