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

/**
 * The method read a XML from URL, skips irrelevant chars and serves back the content as string. 
 * @param inputStream/* w w w.j a  v  a2 s .  com*/
 * @return String : content of a file 
 * @throws IOException
 */
public static String getURLToString(URL url) throws IOException {
    InputStreamReader inputStream = new InputStreamReader(url.openStream(), "ISO-8859-1");
    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();
        }

        String x = byteArrayOutputStream.toString("UTF-8");
        return x;

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

    } finally {
        inputStream.close();
    }
}

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

protected static void writeInt(ByteArrayOutputStream stream, int ival) {
    for (int i = 24; i >= 0; i -= 8)
        stream.write((byte) (ival >>> i));
}

From source file:rs.htec.rest.entities.services.FileUpload.java

public static String getContent(HttpEntity entity) throws IOException {
    if (entity == null) {
        return null;
    }/*w w  w . ja  va2 s  .  c  o  m*/
    InputStream is = entity.getContent();
    try {
        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

public static byte[] read(Context context, String fileName) throws FileNotFoundException, IOException {

    File file = getFile(context, fileName);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    if (file != null) {
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);//w ww .  j a v  a  2  s .c o m
        bos.write(data);
        fis.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

public static byte[] decrypt(byte[] byteArray, PublicKey publicKey) {
    Cipher cipher = null;//from  ww w . ja  v a 2  s . c o  m

    try {
        cipher = 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.DECRYPT_MODE, publicKey);

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

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

    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[128];
            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:Main.java

public static void writeStringSmart(ByteArrayOutputStream baos, String s, Map<String, Integer> knownStrings) {

    if (s == null) {
        baos.write(STRING_NULL);
        return;// w  ww  . jav  a 2  s .  c  om
    }

    // String from list
    if (knownStrings.get(s) != null) {
        baos.write(STRING_FROM_LIST);
        writeLength(baos, knownStrings.get(s));
        return;
    }
    // String prefix from list
    int index = -1;
    String bestPrefix = "";
    for (String prefix : knownStrings.keySet()) {
        if (s.startsWith(prefix) && prefix.length() >= bestPrefix.length()) {
            bestPrefix = prefix;
            index = knownStrings.get(prefix);
        }
    }
    if (index != -1) {
        String rest = s.substring(bestPrefix.length());

        if (worthToCompress(rest)) {
            baos.write(STRING_PREFIX_FROM_LIST_COMPRESSED);
            writeLength(baos, index);
            writeStringCompressed(baos, rest);
        } else {
            baos.write(STRING_PREFIX_FROM_LIST);
            writeLength(baos, index);
            writeString(baos, rest);
        }

        return;
    }

    if (worthToCompress(s)) {
        baos.write(STRING_COMPRESSED);
        writeStringCompressed(baos, s);
        return;
    }
    // Write out whole string
    baos.write(STRING_FROM_BYTES);
    writeString(baos, s);
    System.err.println("Unlisted string: " + s);
}

From source file:com.nxp.ltsm.ltsmclient.tools.VCDescriptionFile.java

public static byte[] deleteVc(String appName, short VC_Entry) {
    String TAG = "VCDescriptionFile:deleteVc";
    Log.i(TAG, "Enter");
    byte[] VC_EntryBytes = Utils.shortToByteArr(VC_Entry);
    try {/*  www  . j a  v a 2s . co m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        out.write(createTlv(0x4F, Hex.decodeHex(appName.toCharArray())));
        out.write(createTlv(0x40, VC_EntryBytes));
        Log.i(TAG, "Exit");
        return createTlv(0x71, out.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Apply some indentiation to some XML. This method is not very sophisticated and will
 * not cope well with anything but the simplest XML (no CDATA etc). The algorithm used does
 * not look at element names and does not actually parse the XML. It also assumes that the
 * forward slash and greater-than at the end of a self-terminating tag and not seperated by
 * ant whitespace.// w  ww.  ja v a  2s  . c  o  m
 *
 * @param xmlString input XML fragment
 * @return indented XML fragment
 */
public static String indentXmlSimple(String xmlString) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int indent = 0;
    byte[] bytes = xmlString.getBytes();
    int i = 0;
    while (i < bytes.length) {
        if (bytes[i] == '<' && bytes[i + 1] == '/') {
            os.write('\n');
            writeIndentation(os, --indent);
        } else if (bytes[i] == '<') {
            if (i > 0) {
                os.write('\n');
            }
            writeIndentation(os, indent++);
        } else if (bytes[i] == '/' && bytes[i + 1] == '>') {
            indent--;
        } else if (bytes[i] == '>') {

        }
        os.write(bytes[i++]);
    }
    return os.toString();
}

From source file:XmlUtil.java

/**
 * Apply some indentiation to some XML. This method is not very
 * sophisticated and will not cope well with anything but the simplest XML
 * (no CDATA etc). The algorithm used does not look at element names and
 * does not actually parse the XML. It also assumes that the forward slash
 * and greater-than at the end of a self-terminating tag and not seperated
 * by ant whitespace./*  www. j a v  a  2 s  .com*/
 * 
 * @param xmlString
 *            input XML fragment
 * @return indented XML fragment
 */
public static String indentXmlSimple(String xmlString) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int indent = 0;
    char bytes[] = xmlString.toCharArray();
    int i = 0;
    while (i < bytes.length) {
        if (bytes[i] == '<' && bytes[i + 1] == '/') {
            os.write('\n');
            writeIndentation(os, --indent);
        } else if (bytes[i] == '<') {
            if (i > 0) {
                os.write('\n');
            }
            writeIndentation(os, indent++);
        } else if (bytes[i] == '/' && bytes[i + 1] == '>') {
            indent--;
        } else if (bytes[i] == '>') {

        }
        os.write(bytes[i++]);
    }
    return os.toString();
}

From source file:com.nxp.ltsm.ltsmclient.tools.VCDescriptionFile.java

public static byte[] CreateVCResp(short vCEntry, byte[] VC_UID) {
    byte[] VC_EntryBytes = Utils.shortToByteArr(vCEntry);
    byte[] stat = new byte[] { (byte) 0x90, (byte) 0x00 };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from  w w w .  j  av  a2 s  .c o  m*/
        out.write(createTlv(0x40, VC_EntryBytes));
        out.write(createTlv(0x41, VC_UID));
        out.write(createTlv(0x4E, stat));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return out.toByteArray();
}