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:Main.java

/**
 * Converts a String SJIS or JIS URL encoded hex encoding to a Unicode String
 * //from w  w w.j  a  v  a2  s .  c om
 */
public static String convertJISEncoding(String target) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (target == null)
        return null;
    String paramString = target.trim();

    for (int loop = 0; loop < paramString.length(); loop++) {
        int i = (int) paramString.charAt(loop);
        bos.write(i);
    }
    String convertedString = null;
    try {
        convertedString = new String(bos.toByteArray(), "JISAutoDetect");
    } catch (java.io.UnsupportedEncodingException uex) {
    }
    return convertedString;
}

From source file:Main.java

public static String executeCommand(String[] args) {
    String result = new String();
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    Process process = null;/*ww  w  .ja v a  2s . co  m*/
    InputStream errIs = null;
    InputStream inIs = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = -1;
        process = processBuilder.start();
        errIs = process.getErrorStream();
        while ((read = errIs.read()) != -1) {
            baos.write(read);
        }
        baos.write('\n');
        inIs = process.getInputStream();
        while ((read = inIs.read()) != -1) {
            baos.write(read);
        }
        byte[] data = baos.toByteArray();
        result = new String(data);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        try {
            if (errIs != null) {
                errIs.close();
            }
            if (inIs != null) {
                inIs.close();
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        if (process != null) {
            process.destroy();
        }
    }
    return result;
}

From source file:Main.java

/**
 * Converts the string to a hex representation with leading 0 byte and 2
 * null terminating byte/*from   w  ww  .j  a  v  a  2  s  .c  o  m*/
 *
 * @param String string (String to be converted to hex.)
 * @return byte[] with converted string
 */
public static byte[] stringToHex(String string) {
    // Convert the string to hex
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    byte name_bytes_temp[] = string.getBytes();
    int length = name_bytes_temp.length;
    for (int i = 0; i < (length + 1) * 2; ++i) {
        if (i % 2 == 1 && (i - 1) / 2 < length) {
            stream.write(name_bytes_temp[(i - 1) / 2]);
        } else
            stream.write((byte) 0);
    }

    return stream.toByteArray();
}

From source file:com.vmware.photon.controller.api.frontend.lib.ova.OvaTestModule.java

/**
 * Read a binary stream as string./*from   ww  w . j a v  a 2 s.  co  m*/
 *
 * @param stream
 * @return
 * @throws java.io.IOException
 */
public static String readStringFromStream(InputStream stream) throws IOException {
    ByteArrayOutputStream byteString = new ByteArrayOutputStream();
    int c;
    int i = 0;
    while ((c = stream.read()) != -1) {
        byteString.write(c);
        byte[] b = new byte[i + i];
        int readBytes = stream.read(b, i, i);
        for (int k = 0; k < readBytes; k++) {
            byteString.write(b[k + i]);
        }
        if (readBytes != i) {
            i = i / 2;
        } else {
            i = i + 1;
        }
    }
    return byteString.toString();
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String decryptStringImpl(Context context, final String encryptedText) {
    String plainText = null;//from www .j a v a  2 s .  c  o m
    try {
        final KeyStore keyStore = getKeyStore(context);

        PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null);

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        CipherInputStream cipherInputStream = new CipherInputStream(
                new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int b;
        while ((b = cipherInputStream.read()) != -1) {
            outputStream.write(b);
        }
        outputStream.close();
        plainText = outputStream.toString("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return plainText;
}

From source file:com.vmware.identity.openidconnect.client.TestUtils.java

static String convertToBase64PEMString(X509Certificate x509Certificate) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byteArrayOutputStream.write("-----BEGIN CERTIFICATE-----".getBytes());
    byteArrayOutputStream.write("\n".getBytes());
    byteArrayOutputStream.write(Base64Utils.encodeToBytes(x509Certificate.getEncoded()));
    byteArrayOutputStream.write("-----END CERTIFICATE-----".getBytes());
    byteArrayOutputStream.write("\n".getBytes());
    return byteArrayOutputStream.toString();
}

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

protected static void writeToStream(byte[] v, ByteArrayOutputStream out) throws IOException {
    if (v == null) {
        writeInt32ToStream(0, out);//from   w w w  .  j a va  2  s. c o  m
    } else {
        writeInt32ToStream(v.length + 1, out);
        out.write(v);
        out.write(0); //???0,byte[]?0
    }
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] CompressGLZ(InputStream in) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[4];
    // BinConverter.intToByteArray(0,buf,0);
    baos.write(buf);
    GLZ glz = new GLZ();
    long total = glz.compress(in, baos);
    byte[] ret = baos.toByteArray();
    total &= 0x1fffffff;/*from  ww  w  .  j ava2  s  . co m*/
    total |= GLZ;
    BinConverter.intToByteArray((int) total, ret, 0);
    return ret;

}

From source file:info.bonjean.beluga.util.ResourcesUtil.java

private static byte[] getResourceAsByteArray(String resource) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream bais;//from   ww  w .  j a v a 2s  .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:Main.java

public static String getResourceText(Context context, int resId) {
    InputStream is = null;/*from  w  w  w . ja v  a 2  s. c o m*/
    BufferedInputStream bis;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        is = context.getResources().openRawResource(resId);
        bis = new BufferedInputStream(is);
        int result = bis.read();
        while (result != -1) {
            byte b = (byte) result;
            baos.write(b);
            result = bis.read();
        }
    } catch (IOException e) {
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
    }
    return baos.toString();
}