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[] argv) throws Exception {
    byte[] b = new byte[1];
    Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost", "proxy.mydomain.local");
    systemSettings.put("http.proxyPort", "80");

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    BASE64Encoder encoder = new BASE64Encoder();
    String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
    con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
    DataInputStream di = new DataInputStream(con.getInputStream());
    while (-1 != di.read(b, 0, 1)) {
        System.out.print(new String(b));
    }/*from   ww  w .  ja  v  a  2 s . c o  m*/
}

From source file:MainClass.java

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

    byte[] input = "abc".getBytes();
    Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
    SecureRandom random = new SecureRandom();
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(386, random);//from  w w  w  .  java 2 s .c  o m

    KeyPair pair = generator.generateKeyPair();
    Key pubKey = pair.getPublic();

    Key privKey = pair.getPrivate();

    cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
    byte[] cipherText = cipher.doFinal(input);
    System.out.println("cipher: " + new String(cipherText));

    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plainText = cipher.doFinal(cipherText);
    System.out.println("plain : " + new String(plainText));
}

From source file:Test.java

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

    AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
    InetSocketAddress address = new InetSocketAddress("localhost", 5000);

    Future<Void> future = client.connect(address);
    System.out.println("Client: Waiting for the connection to complete");
    future.get();/* w ww  .ja va 2  s . com*/

    String message = "";
    while (!message.equals("quit")) {
        System.out.print("Enter a message: ");
        Scanner scanner = new Scanner(System.in);
        message = scanner.nextLine();
        System.out.println("Client: Sending ...");
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
        System.out.println("Client: Message sent: " + new String(buffer.array()));
        client.write(buffer);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY );");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    // create some binary data
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();

    // set value for the prepared statement
    pstmt.setBytes(1, binaryData);// w w w .  j av  a  2  s . c  o  m

    // insert the data
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(new String(rs.getBytes(2)));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] b = new byte[1];
    Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost", "proxy.mydomain.local");
    systemSettings.put("http.proxyPort", "80");

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("mydomain\\username", "password".toCharArray());
        }// w  w w  .  j  av  a2 s  . c  o  m
    });

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    DataInputStream di = new DataInputStream(con.getInputStream());
    while (-1 != di.read(b, 0, 1)) {
        System.out.print(new String(b));
    }
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";
    byte[] b = { 'e', 'x', 'a', 'm', 'p', 'l', 'e' };

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

    // write something in the file
    oout.writeObject(s);//  w  ww .ja v a  2s  .  com
    oout.writeObject(b);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readObject());

    // read and print an object and cast it as string
    byte[] read = (byte[]) ois.readObject();
    String s2 = new String(read);
    System.out.println(s2);
    ois.close();
}

From source file:com.woooha.FooTest.java

public static void main(String[] args) throws UnsupportedEncodingException, JSONException {
    JSONArray result = new JSONArray("[{}]");
    System.out.println(result.get(0).getClass());
    byte[] bytes = "http://192.168.1.5:3477/woooha/upload/music/x13939426.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
    bytes = "http://192.168.1.5:3477/woooha/upload/music/x13850595.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
    bytes = "http://192.168.1.5:3477/woooha/upload/music/x13918712.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
}

From source file:algoritmorsa_md5.MD5.java

/**
 * @param args the command line arguments
 *//*from   w  w w .j ava2s  . c o m*/
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO code application logic here

    MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
    md.update("yesica".getBytes());
    byte[] digest = md.digest();

    for (byte b : digest) {
        System.out.println(Integer.toHexString(0xFF & b));
    }
    System.out.println();
    byte[] encoded = Base64.encodeBase64(digest);
    System.out.println(new String(encoded));
}

From source file:icevaluation.BingAPIAccess.java

public static void main(String[] args) {
    String searchText = "arts site:wikipedia.org";
    searchText = searchText.replaceAll(" ", "%20");
    // String accountKey="jTRIJt9d8DR2QT/Z3BJCAvY1BfoXj0zRYgSZ8deqHHo";
    String accountKey = "JfeJSA3x6CtsyVai0+KEP0A6CYEUBT8VWhZmm9CS738";

    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    URL url;//from   www  .ja v  a2s.c  o m
    try {
        url = new URL("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27Web%27&Query=%27"
                + searchText + "%27&$format=JSON");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        System.out.println("Output from Server .... \n");
        //write json to string sb
        int c = 0;
        if ((output = br.readLine()) != null) {
            System.out.println("Output is: " + output);
            sb.append(output);
            c++;
            //System.out.println("C:"+c);

        }

        conn.disconnect();
        //find webtotal among output      
        int find = sb.indexOf("\"WebTotal\":\"");
        int startindex = find + 12;
        System.out.println("Find: " + find);

        int lastindex = sb.indexOf("\",\"WebOffset\"");

        System.out.println(sb.substring(startindex, lastindex));

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

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));
}