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(byte b[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // write byte array to the output stream
    baos.write(bs, 3, 2);

    // read all the bytes in the output stream
    for (byte b : baos.toByteArray()) {
        System.out.println(b);//from  www . ja  v  a 2  s.c o  m
    }

}

From source file:Main.java

public static void main(String[] arg) throws Throwable {
    File f = new File(arg[0]);
    InputStream in = new FileInputStream(f);

    byte[] buff = new byte[8000];

    int bytesRead = 0;

    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = in.read(buff)) != -1) {
        bao.write(buff, 0, bytesRead);
    }//from  www  .j a  v  a  2s. c om
    in.close();

    byte[] data = bao.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    System.out.println(bin.available());
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    URL url = new URL("http://www.java2s.com/style/download.png");

    InputStream inputStream = url.openStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int n = 0;//  w ww . j a  v a 2s  . c om
    while (-1 != (n = inputStream.read(buffer))) {
        output.write(buffer, 0, n);
    }
    inputStream.close();

    byte[] data = output.toByteArray();

    OutputStream out = new FileOutputStream("data.png");
    out.write(data);
    out.close();

    for (byte b : data) {
        System.out.printf("0x%x ", b);
    }
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    byte[] input = "asdf".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*from   w ww. j a  v a2 s. c  om*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] compressedData = null;
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);

    }/*from w w w  .ja v  a2s .c  o  m*/
    bos.close();
    byte[] decompressedData = bos.toByteArray();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "www.java2s.com".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*w ww  .ja  v a  2  s. com*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
    System.out.println(Arrays.toString(compressedData));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "this is a test".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*from w ww.  j av  a 2  s  . c  om*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] compressedData = bos.toByteArray();
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    bos = new ByteArrayOutputStream(compressedData.length);

    buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] decompressedData = bos.toByteArray();
    System.out.println(new String(decompressedData));
}

From source file:Hex.java

public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: HexStrToBin enc/dec <infileName> <outfilename>");
        System.exit(1);//from   w w w .  j a  va  2 s. c o m
    }
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream in = new FileInputStream(args[1]);
        int len = 0;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf)) > 0)
            os.write(buf, 0, len);
        in.close();
        os.close();

        byte[] data = null;
        if (args[0].equals("dec"))
            data = decode(os.toString());
        else {
            String strData = encode(os.toByteArray());
            data = strData.getBytes();
        }

        FileOutputStream fos = new FileOutputStream(args[2]);
        fos.write(data);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.cloudkit.enterprises.ws.SuperPassParaProxyTest.java

public static void main(String[] args) throws Exception {

    // CodeLists_Const???XML?
    // MFT2008? RMFT8ChangeReasonCode (AlphaNumber)
    // 001   ?/* www.  ja v  a  2  s . c om*/
    // 002   
    // 003   ?
    // 004   ????
    // 005   ????
    // 006   ??????
    // 007   ????
    // 008   ????
    // 009   ??????
    // 010   ????
    // 011   ???
    // 012   ?
    // 013   ?
    // 014   ?
    // 015   ??
    // 999   
    //
    // MFT2008 RMFT8DeclareTypeCode (AlphaNumber)
    // MT1401   ??
    // MT2401   ???
    // MT5401   ??
    // MT5402   ??
    // MT3402   ?
    // MT7402   ??
    // MT8401   ??
    // MT8402   ??
    // MT8403   ???
    // MT8404   ???
    // MT4401   ??
    // MT4402   ??
    // MT4403   ??
    // MT4404   ??
    // MT4405   ??
    // MT4406   ??
    //
    // MFT2008 RMFT8DeclareTypeCodeStat (AlphaNumber)
    // MT1401   ??
    // MT2401   ???
    // MT5401   ??
    // MT5402   ??
    // MT3402   ?
    // MT7402   ??
    // MT8401   ??
    // MT8402   ??
    // MT8403   ???
    // MT8404   ???
    // MT4401   ??
    // MT4402   ??
    // MT4403   ??
    // MT4404   ??
    // MT4405   ??
    // MT4406   ??
    //
    // MFT2008 RMFT8FunctionTypeCode (AlphaNumber)
    // 2   
    // 3   
    // T   
    // 5   ?
    // 9   ???
    // 0   ???
    // 11   
    //
    // MFT2008 (AlphaNumber) RMFT8ReponseTypeCode
    // M1   M1-
    // M2   M2-??
    // M3   QP
    // 01   01-?
    // 02   02-
    // 03   03-?
    // 11   11-
    // 12   12-
    // 13   13-??
    // T   T-
    // D   D-?
    // E1   E1-??
    // E2   E2-?
    // E3   E3-??
    // E4   E4-?
    //
    // ?  (AlphaNumber) MasterAffirmSign
    // 0   
    // 1   ?
    // 2   ?
    //
    // ? ? (AlphaNumber) DeclareType
    // 0   
    // 1   
    //
    // MFT2008?? RMFT8MobileTypeCode (AlphaNumber)
    // MT4403   ??
    // MT4404   ??
    // MT4405   ??
    // MT4406   ??
    //
    // ?  RMFT8MobileFuncCode (AlphaNumber)
    // M1   
    // M2   ??
    // M3   QP
    // 2   
    // 3   
    // T   
    // 5   ?
    // 9   ?
    // 0   (??)
    // 11   

    URL url = new URL("http://ceesb.chinaport.gov.cn/SuperPassParaProxy/Proxy_Services/SuperPass_Proxy?wsdl");
    QName qname = new QName("http://www.cneport.com/webservices/superpass", "SuperPass");
    Service service = Service.create(url, qname);
    SuperPass superPass = service.getPort(SuperPass.class);

    /*
    String serviceName = "eport.superpass.pub.para.CustomsFlagStatus";
    byte[] requestContext = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><RequestContext><Group name=\"SystemInfo\"><Key name=\"ClientId\">5300001128334</Key><Key name=\"CertNo\">c1f4bb</Key><Key name=\"SaicSysNo\">618882068</Key><Key name=\"DEP_IN_CODE\">5300</Key><Key name=\"REG_CO_CGAC\">4403941436</Key><Key name=\"ENT_SEQ_NO\">000000000000063462</Key><Key name=\"IcCode\">8800000246746</Key><Key name=\"OperatorName\"></Key><Key name=\"DEP_CODE_CHG\">5300</Key><Key name=\"NAME_FULL\">???</Key></Group><Group name=\"DataPresentation\"><Key name=\"SignatureAlgorithm\" /><Key name=\"EncryptAlgorithm\" /><Key name=\"CompressAlgorithm\" /></Group></RequestContext>".getBytes();
    byte[] requestData = "<?xml version=\"1.0\"?>\n<CustomsFlagStatusRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n<CustomsCode>5300</CustomsCode>\n</CustomsFlagStatusRequest>".getBytes();
    */

    // ??
    String serviceName = "eport.superpass.pub.para.LoadIntoMemory";
    byte[] requestContext = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><RequestContext><Group name=\"SystemInfo\"><Key name=\"ClientId\">5300001128334</Key><Key name=\"CertNo\">b6c29b</Key><Key name=\"SaicSysNo\">618882068</Key><Key name=\"DEP_IN_CODE\">5300</Key><Key name=\"REG_CO_CGAC\">4403941436</Key><Key name=\"ENT_SEQ_NO\">000000000000063462</Key><Key name=\"SessionId\">2015-7-14</Key><Key name=\"IcCode\">8910000270086</Key><Key name=\"OperatorName\">??</Key><Key name=\"DEP_CODE_CHG\">5300</Key><Key name=\"NAME_FULL\">???</Key></Group><Group name=\"DataPresentation\"><Key name=\"SignatureAlgorithm\" /><Key name=\"EncryptAlgorithm\" /><Key name=\"CompressAlgorithm\" /></Group></RequestContext>"
            .getBytes();
    // ? MFT8TrayType
    // SELECT TRAY_CODE,TRAY_NAME FROM MFT8_TRAY_TYPE ORDER BY TRAY_CODE
    // ??? RMFT8TransportType
    // SELECT CODE, NAME FROM mft8_transport_type ORDER BY CODE
    // ? RMFT8CustomsCode
    // SELECT customs_CODE, customs_NAME FROM customs ORDER BY customs_CODE
    // ??(CN003) RMFT8PortCode
    // SELECT CODE, CHNAME FROM mft8_location_name ORDER BY CODE
    // ? RMFT8Curr
    // SELECT CODE, NAME FROM MFT8_ROAD_CURR ORDER BY CODE
    // ? RMFT8ReceiptPlace
    // SELECT CODE, NAME FROM mft8_entity ORDER BY CODE
    // ? RMFT8CountryCode
    // SELECT CODE, NAME FROM mft8_country_code ORDER BY CODE
    // ?? RMFT8CustomStatus
    // SELECT CODE, NAME FROM MFT8_ROAD_CUSTOMS_STATUS ORDER BY CODE
    // ?? RMFT8TransPayCode
    // SELECT CODE, NAME FROM MFT8_ROAD_PAYMENT_METHOD ORDER BY CODE
    // ??(CN005) RMFT8WrapTypeCode
    // SELECT CODE, NAME FROM mft8_packaging ORDER BY CODE
    // ??? RMFT8TransLicCode
    // SELECT CODE, NAME FROM MFT8_CONTR_CAR_COND ORDER BY CODE
    // ?? RMFT8PortWorkCode
    // SELECT CODE, NAME FROM MFT8_HAND_INSTR ORDER BY CODE
    // ?? RMFT8CommunicationCode
    // SELECT CODE, NAME FROM mft8_communi_type ORDER BY CODE
    // ? RMFT8PostCode
    // SELECT CODE, NAME FROM mft8_post_code ORDER BY CODE
    //  RMFT8EquipSizeCode
    // SELECT CODE, SHAPE,LENGTH,HEIGHT,WIDTH FROM mft8_equip_size_type ORDER BY CODE
    // ??? RMFT8EquipSupCode
    // SELECT CODE, NAME FROM MFT8_EQUIP_SUP ORDER BY CODE
    // ?? RMFT8EquipFullCode
    // SELECT CODE, NAME FROM mft8_equip_full ORDER BY CODE
    // ?? RMFT8EquipSealCode
    // SELECT CODE, NAME FROM mft8_seal_agency ORDER BY CODE
    // ??? RMFT8DanGoodsCode
    // SELECT CODE, CHNAME FROM mft8_dan_goods ORDER BY CODE
    // ? RMFT8CusProCode
    // SELECT CODE, NAME FROM mft8_cus_procedure ORDER BY CODE
    // ?/? RMFT8TransportSplitCode
    // SELECT CODE, NAME FROM mft8_indication ORDER BY CODE
    // ?? RMFT8DamageAreaCode
    // SELECT CODE, NAME FROM MFT8_DAMAGE_AREA ORDER BY CODE
    // ?? RMFT8DamageTypeCode
    // SELECT CODE, NAME FROM MFT8_DAMAGE_TYPE_DES ORDER BY CODE
    // IATA?(UN005) RMFT8IATACode
    // SELECT CODE, NAME FROM mft8_iata_code ORDER BY CODE
    // ????(UN009) RMFT8LocodePort
    // SELECT CODE, NAME FROM mft8_un_locode_port ORDER BY CODE
    // RMFT8ComplexCode
    // SELECT CODE_TS, G_NAME FROM complex ORDER BY CODE_TS
    byte[] requestData = "<?xml version=\"1.0\"?>\n<LoadIntoMemoryRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n<QuerySql>SELECT CODE, NAME FROM mft8_un_locode_port ORDER BY CODE</QuerySql>\n</LoadIntoMemoryRequest>"
            .getBytes();

    Holder<byte[]> responseData = new Holder<byte[]>();
    System.out.println(new String(superPass.service(serviceName, requestContext, requestData, responseData)));

    // deflate 
    DeflateCompressorInputStream gis = new DeflateCompressorInputStream(
            new ByteArrayInputStream(responseData.value));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int count;
    byte data[] = new byte[1024];
    while ((count = gis.read(data, 0, 1024)) != -1) {
        baos.write(data, 0, count);
    }
    gis.close();

    data = baos.toByteArray();
    baos.flush();
    baos.close();

    System.out.println(new String(data));
    // System.out.println(new String(responseData.value, "UTF-8"));
}

From source file:com.iflytek.spider.util.EncodingDetector.java

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: EncodingDetector <file>");
        System.exit(1);/*from  www .  jav  a 2  s .  c o  m*/
    }

    Configuration conf = SpiderConfiguration.create();
    EncodingDetector detector = new EncodingDetector(SpiderConfiguration.create());

    // do everything as bytes; don't want any conversion
    BufferedInputStream istr = new BufferedInputStream(new FileInputStream(args[0]));
    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    byte[] bytes = new byte[1000];
    boolean more = true;
    while (more) {
        int len = istr.read(bytes);
        if (len < bytes.length) {
            more = false;
            if (len > 0) {
                ostr.write(bytes, 0, len);
            }
        } else {
            ostr.write(bytes);
        }
    }

    byte[] data = ostr.toByteArray();

    // make a fake Content
    Content content = new Content("", data, null);

    detector.autoDetectClues(content, true);
    String encoding = detector.guessEncoding(content, conf.get("parser.character.encoding.default"));
    System.out.println("Guessed encoding: " + encoding);
}