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

protected static byte[] getWapTextString(ByteArrayInputStream bais) {
    assert (null != bais);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    int temp = bais.read();
    assert (-1 != temp);

    while ((-1 != temp) && ('\0' != temp)) {
        if (isText(temp)) {
            bos.write(temp);
        }/*  w  w w .j ava2  s .  com*/
        temp = bais.read();
        assert (-1 != temp);
    }

    if (bos.size() > 0) {
        return bos.toByteArray();
    }

    return null;
}

From source file:org.apache.jetspeed.util.Base64.java

public static byte[] decodeAsByteArray(String b64string) throws Exception {
    InputStream in = MimeUtility.decode(new ByteArrayInputStream(b64string.getBytes()), "base64");

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    while (true) {
        int b = in.read();
        if (b == -1)
            break;
        else/*from w w  w .jav  a2  s  .  c  o m*/
            out.write(b);
    }

    return out.toByteArray();
}

From source file:net.officefloor.plugin.servlet.container.WebInfResourceAccessTest.java

/**
 * Asserts the resource.//from  w w w .  j  a  va 2 s .com
 * 
 * @param request
 *            {@link HttpServletRequest}.
 * @param resourcePath
 *            Path to the resource.
 * @param expectedResourceContent
 *            Expected content of the resource.
 */
private static void assertResource(HttpServletRequest request, String resourcePath,
        String expectedResourceContent) throws IOException {

    // Validate obtain the resource
    InputStream resource = request.getServletContext().getResourceAsStream(resourcePath);
    assertNotNull("Should have " + resourcePath + " resource", resource);

    // Validate content of resource
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int value = resource.read(); value != -1; value = resource.read()) {
        buffer.write(value);
    }
    String resourceContent = new String(buffer.toByteArray());
    assertEquals("Incorrect content for resource " + resourcePath, expectedResourceContent, resourceContent);
}

From source file:eu.europa.esig.dss.xades.signature.DSSSignatureUtils.java

/**
 * Converts an ASN.1 ECDSA value to a XML Signature ECDSA Value.
 *
 * The JAVA JCE ECDSA Signature algorithm creates ASN.1 encoded (r,s) value pairs; the XML Signature requires the
 * core BigInteger values.//  w w w.j a  v  a2  s  .  co m
 *
 * @param binaries
 *            the ASN1 signature value
 * @return the decode bytes
 * @throws IOException
 * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#dsa-sha1">6.4.1 DSA</A>
 * @see <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc4050.txt">3.3. ECDSA Signatures</A>
 */
private static byte[] convertECDSAASN1toXMLDSIG(byte[] binaries) {
    ASN1InputStream is = null;
    try {
        is = new ASN1InputStream(binaries);

        ASN1Sequence seq = (ASN1Sequence) is.readObject();
        if (seq.size() != 2) {
            throw new IllegalArgumentException("ASN1 Sequence size should be 2 !");
        }
        ASN1Integer r = (ASN1Integer) seq.getObjectAt(0);
        ASN1Integer s = (ASN1Integer) seq.getObjectAt(1);

        byte[] rBytes = r.getValue().toByteArray();
        int rSize = rBytes.length;
        byte[] sBytes = s.getValue().toByteArray();
        int sSize = sBytes.length;

        int max = Math.max(rSize, sSize);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream(max * 2);
        if (sSize > rSize) {
            buffer.write(0x00);
        }
        buffer.write(rBytes);
        if (rSize > sSize) {
            buffer.write(0x00);
        }
        buffer.write(sBytes);
        return buffer.toByteArray();
    } catch (Exception e) {
        throw new DSSException("Unable to convert to xmlDsig : " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

public static List<ByteArrayOutputStream> cutby(int value, byte[] content) {
    List<ByteArrayOutputStream> bytes = new ArrayList<ByteArrayOutputStream>();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean preisSame = false; //
    if (content.length > 0) {
        for (int i = 0; i < content.length; i++) {
            if (content[i] != value) {
                out.write(content[i]);
                preisSame = false;/* w ww .  ja v a 2 s. c  o m*/
            } else if (!preisSame) {
                if (out.size() > 0) {
                    bytes.add(out);
                    out = new ByteArrayOutputStream();
                }
                preisSame = true;
            }
        }
    }
    if (out.size() > 0) {
        bytes.add(out);
    }
    return bytes;
}

From source file:com.ibasco.agql.examples.base.BaseExample.java

private static byte[] padNullBytes(String text) {
    if (StringUtils.isEmpty(text))
        return null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*w  w w . j a  v  a 2 s .  co  m*/
        bos.write(text.getBytes("UTF-8"));
        while ((bos.size() % 16) != 0) {
            bos.write(0);
        }
    } catch (IOException e) {
        throw new AsyncGameLibUncheckedException(e);
    }
    return bos.toByteArray();
}

From source file:org.jboss.pnc.auth.ExternalAuthentication.java

public static String getContent(HttpEntity entity) throws IOException {
    if (entity == null)
        return null;
    InputStream is = entity.getContent();
    try {//from   www .  j a v  a2 s  .  com
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int c;
        while ((c = is.read()) != -1) {
            os.write(c);
        }
        byte[] bytes = os.toByteArray();
        String data = new String(bytes);
        return data;
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {

        }
    }

}

From source file:Main.java

/**
 * The method read XML from a file, skips irrelevant chars and serves back the content as string. 
 * @param inputStream//  ww  w.j a  v  a  2 s.co m
 * @return String : content of a file 
 * @throws IOException
 */
public static String getFileToString(Context context, String xml_file) throws IOException {
    InputStream inputStream = context.getAssets().open(xml_file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {
        int i = inputStream.read();
        while (i != -1) {
            if (i != TAB && i != NL && i != CR)
                byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        return byteArrayOutputStream.toString("UTF-8");

    } catch (IOException e) {
        e.printStackTrace();
        return "";

    } finally {
        inputStream.close();
    }
}

From source file:Main.java

public static byte[] encrypt(byte[] byteArray, PrivateKey privateKey) {
    Cipher cipher = null;/*from  www.j  av a  2 s. c  o  m*/

    try {
        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        /*
           (define cipher (javax.crypto.Cipher.getInstance "RSA/ECB/NoPadding"))
         */
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[100];
            input.read(t0);
            output.write(cipher.doFinal(t0));
        }
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

protected static void writeLong(ByteArrayOutputStream stream, long l) {
    for (int i = 56; i >= 0; i -= 8)
        stream.write((byte) (l >>> i));
}