Example usage for java.lang String getBytes

List of usage examples for java.lang String getBytes

Introduction

In this page you can find the example usage for java.lang String getBytes.

Prototype

public byte[] getBytes() 

Source Link

Document

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Usage

From source file:com.newlandframework.avatarmq.test.AvatarMQProducer2.java

public static void main(String[] args) throws InterruptedException {
    AvatarMQProducer producer = new AvatarMQProducer("127.0.0.1:18888", "AvatarMQ-Topic-2");
    producer.setClusterId("AvatarMQCluster2");
    producer.init();/* ww w. j a v a 2  s  .co m*/
    producer.start();

    System.out.println(StringUtils.center("AvatarMQProducer2 ???", 50, "*"));

    for (int i = 0; i < 100; i++) {
        Message message = new Message();
        String str = "Hello AvatarMQ From Producer2[" + i + "]";
        message.setBody(str.getBytes());
        ProducerAckMessage result = producer.delivery(message);
        if (result.getStatus() == (ProducerAckMessage.SUCCESS)) {
            System.out.printf("AvatarMQProducer2 ????:%s\n", result.getMsgId());
        }

        Thread.sleep(100);
    }

    producer.shutdown();
    System.out.println(StringUtils.center("AvatarMQProducer2 ???", 50, "*"));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    InetAddress server = InetAddress.getByName("localhost");
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    DatagramSocket theSocket = new DatagramSocket();
    while (true) {
        String theLine = userInput.readLine();
        if (theLine.equals("."))
            break;
        byte[] data = theLine.getBytes();
        DatagramPacket theOutput = new DatagramPacket(data, data.length, server, 99999);
        theSocket.send(theOutput);/*from  w ww .  ja  v  a2 s . co m*/
    }

}

From source file:de.tum.i13.ConvertCsvToProtobuf.java

public static void main(String args[]) {
    try {/*from  ww  w .  j a v  a  2  s  . co  m*/
        LineIterator it = FileUtils.lineIterator(new File("/Users/manit/Projects/sdcbenchmark/Dataset/debscsv"),
                "UTF-8");
        FileOutputStream out = new FileOutputStream("/Users/manit/Projects/sdcbenchmark/Dataset/debsprotobuf",
                true);

        while (it.hasNext()) {

            String csvLine = (String) it.next();
            byte[] csvLineBytes = csvLine.getBytes();
            String line = new String(csvLineBytes, StandardCharsets.UTF_8);
            Debs2015Protos.Taxitrip.Builder builder = Debs2015Protos.Taxitrip.newBuilder();
            String[] splitted = line.split(",");

            builder.setMedallion(splitted[0]);
            builder.setHackLicense(splitted[1]);
            builder.setPickupDatetime(splitted[2]);
            builder.setDropoffDatetime(splitted[3]);
            builder.setTripTimeInSecs(Integer.parseInt(splitted[4]));
            builder.setTripDistance(Float.parseFloat(splitted[5]));
            builder.setPickupLongitude(Float.parseFloat(splitted[6]));
            builder.setPickupLatitude(Float.parseFloat(splitted[7]));
            builder.setDropoffLongitude(Float.parseFloat(splitted[8]));
            builder.setDropoffLatitude(Float.parseFloat(splitted[9]));
            builder.setPaymentType(splitted[10]);
            builder.setFareAmount(Float.parseFloat(splitted[11]));
            builder.setSurcharge(Float.parseFloat(splitted[12]));
            builder.setMtaTax(Float.parseFloat(splitted[13]));
            builder.setTipAmount(Float.parseFloat(splitted[14]));
            builder.setTollsAmount(Float.parseFloat(splitted[15]));
            builder.setTotalAmount(Float.parseFloat(splitted[16]));

            builder.build().writeDelimitedTo(out);
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:DataStudioCrak.java

public static void main(String[] args) {
    String company = "labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {// w  w w . j a va2 s .  com
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

From source file:net.labthink.run.DataStudioCrak.java

public static void main(String[] args) {
    String company = "Labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {//ww w . j  av  a 2s. c  om
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

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

public static void main(String[] args) {

    Path rn_demo = Paths.get("C:/tutorial/Java", "demo.txt");
    String demo = "tutorial";
    String string = "\nString: from java2s.com";

    //using NIO.2 unbuffered stream
    byte data[] = demo.getBytes();
    try (OutputStream outputStream = Files.newOutputStream(rn_demo)) {
        outputStream.write(data);/*from w  w  w.j  a  v  a 2  s .  co m*/
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:BufferedInputStreamDemo.java

public static void main(String args[]) throws IOException {
    String s = "This is a &copy; copyright symbol " + "but this is &copy not.\n";
    byte buf[] = s.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    BufferedInputStream f = new BufferedInputStream(in);
    int c;/*from ww w .j a va 2  s . co m*/
    boolean marked = false;

    while ((c = f.read()) != -1) {
        switch (c) {
        case '&':
            if (!marked) {
                f.mark(32);
                marked = true;
            } else {
                marked = false;
            }
            break;
        case ';':
            if (marked) {
                marked = false;
                System.out.print("(c)");
            } else
                System.out.print((char) c);
            break;
        case ' ':
            if (marked) {
                marked = false;
                f.reset();
                System.out.print("&");
            } else
                System.out.print((char) c);
            break;
        default:
            if (!marked)
                System.out.print((char) c);
            break;
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Provider p = Security.getProvider("Rot13Provider");
    System.out.println("Provider name: " + p.getName());
    System.out.println("Provider version: " + p.getVersion());
    System.out.println("Provider information: " + p.getInfo());

    Cipher cipher = Cipher.getInstance("ROT13", "Rot13Provider");
    System.out.println("Cipher: " + cipher.getAlgorithm());
    String testString = "This is a test!";
    cipher.init(Cipher.ENCRYPT_MODE, (Key) null, new SecureRandom());
    byte[] b1 = cipher.doFinal(testString.getBytes());
    cipher.init(Cipher.DECRYPT_MODE, (Key) null, new SecureRandom());
    byte[] b2 = cipher.doFinal(b1);
    System.out.println("Decrypted data as a String: " + new String(b2));
}

From source file:com.pinterest.terrapin.client.ClientTool.java

public static void main(String[] args) throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration(System.getProperty("terrapin.config"));
    TerrapinClient client = new TerrapinClient(config, 9090, 1000, 5000);
    String key = args[1];
    TerrapinSingleResponse response = client.getOne(args[0], // fileset
            ByteBuffer.wrap(key.getBytes())).get();
    if (response.isSetErrorCode()) {
        System.out.println("Got error " + response.getErrorCode().toString());
    } else if (response.isSetValue()) {
        System.out.println("Got value.");
        System.out.println(new String(response.getValue()));
    } else {//from  w  ww .  j  a  va2 s  .c o m
        System.out.println("Key " + key + " not found.");
    }
    System.exit(0);
}