Example usage for java.lang String String

List of usage examples for java.lang String String

Introduction

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

Prototype

public String(StringBuilder builder) 

Source Link

Document

Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramChannel client = null;
    client = DatagramChannel.open();

    client.bind(null);/*from w  w  w .  j  a  v  a2 s .  com*/

    String msg = "Hello";
    ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
    InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8989);

    client.send(buffer, serverAddress);
    buffer.clear();
    client.receive(buffer);
    buffer.flip();
    int limits = buffer.limit();
    byte bytes[] = new byte[limits];
    buffer.get(bytes, 0, limits);
    String response = new String(bytes);
    System.out.println("Server  responded: " + response);
    client.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] ivBytes = "1234567812345678".getBytes();

    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
    generator.init(128);//from w  w  w .  ja v  a  2 s  .  c  om
    Key encryptionKey = generator.generateKey();
    System.out.println("key : " + new String(encryptionKey.getEncoded()));

    cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes));
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain : " + new String(plainText));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);/*from   w w w .  j  a  v  a  2s  .  c  o m*/
    out.close();

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
}

From source file:Main.java

public static void main(String[] args) {
    boolean b = true;
    StringBuffer sb1 = new StringBuffer();
    sb1.append(b);//from   w  w w  .  j a va  2s. com
    System.out.println(sb1);

    char c = 'Y';
    sb1.append(c);
    System.out.println(sb1);

    char[] c1 = new char[] { 'Y', 'e', 's' };
    sb1.append(c1);

    System.out.println(sb1);

    double d = 1.0;
    sb1.append(d);
    System.out.println(sb1);

    float f = 1.0f;
    sb1.append(f);
    System.out.println(sb1);

    int i = 1;
    sb1.append(i);
    System.out.println(sb1);

    long l = 1;
    sb1.append(l);

    System.out.println(sb1);

    Object obj = new String("Yes");
    sb1.append(obj);
    System.out.println(sb1);

    String str = new String("Yes");
    sb1.append(str);
    System.out.println(sb1);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = " www.java2s.com ".getBytes();
    byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
            0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
    System.out.println("input text : " + new String(input));

    // encryption pass

    byte[] cipherText = new byte[input.length];
    cipher.init(Cipher.ENCRYPT_MODE, key);
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    System.out.println("cipher text: " + new String(cipherText) + " bytes: " + ctLength);

    // decryption pass

    byte[] plainText = new byte[ctLength];
    cipher.init(Cipher.DECRYPT_MODE, key);
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain text : " + new String(plainText) + " bytes: " + ptLength);
}

From source file:com.java.demo.DesDemo.java

public static void main(String[] args) {
    //des(data crpt)
    Init();/*from w w w  .  ja  v a 2  s.  c  o  m*/
    byte[] strBytes = Encrypt("abc");
    System.out.println(Hex.encodeHexString(strBytes));
    byte[] str = Decrypt(strBytes);
    System.out.println(new String(str));
}

From source file:Main.java

public static void main(String[] args) {
    boolean b = true;
    StringBuffer sb1 = new StringBuffer("1234567890");
    sb1.insert(6, b);/* w w  w.j a v a 2  s  .  com*/
    System.out.println(sb1);

    char c = 'Y';
    sb1.insert(6, c);
    System.out.println(sb1);

    char[] c1 = new char[] { 'Y', 'e', 's' };
    sb1.insert(6, c1);
    System.out.println(sb1);

    double d = 1.0;
    sb1.insert(6, d);
    System.out.println(sb1);

    float f = 2.0f;
    sb1.insert(6, f);
    System.out.println(sb1);

    int i = 5;
    sb1.insert(6, i);
    System.out.println(sb1);

    long l = 10;
    sb1.insert(6, l);
    System.out.println(sb1);

    Object obj = new String("b");
    sb1.insert(6, obj);
    System.out.println(sb1);

    String str = "a";
    sb1.insert(6, str);
    System.out.println(sb1);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] keyBytes = "input123".getBytes();

    SecretKeySpec key = new SecretKeySpec(keyBytes, "ARC4");

    Cipher cipher = Cipher.getInstance("ARC4", "BC");

    byte[] cipherText = new byte[input.length];

    cipher.init(Cipher.ENCRYPT_MODE, key);

    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);

    ctLength += cipher.doFinal(cipherText, ctLength);

    System.out.println("cipher text: " + new String(cipherText));

    byte[] plainText = new byte[ctLength];

    cipher.init(Cipher.DECRYPT_MODE, key);

    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);

    ptLength += cipher.doFinal(plainText, ptLength);

    System.out.println("plain text : " + new String(plainText));
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF(s);/*from  w  ww. j a  v a2s.co  m*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print the whole content
    byte[] b = new byte[13];
    ois.readFully(b);
    String array = new String(b);
    System.out.println(array);
    ois.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] keyBytes = "input123".getBytes();

    SecretKeySpec key = new SecretKeySpec(keyBytes, "ARC4");

    Cipher cipher = Cipher.getInstance("ARC4", "BC");

    byte[] cipherText = new byte[input.length];

    cipher.init(Cipher.ENCRYPT_MODE, key);

    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);

    ctLength += cipher.doFinal(cipherText, ctLength);

    System.out.println("cipher text: " + new String(cipherText));

    byte[] plainText = new byte[ctLength];

    cipher.init(Cipher.DECRYPT_MODE, key);

    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);

    ptLength += cipher.doFinal(plainText, ptLength);

    System.out.println("plain text : " + new String(plainText));
}