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

public static void main(final String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  ww  w . ja  va 2  s.  c om*/
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new ByteArrayInputStream(( //
    "<?xml version=\"1.0\"?>" + //
            "<people>" + //
            "<person><name>First Person Name</name></person>" + //
            "<person><name>Second Person Name</name></person>" + //
            "</people>" //
    ).getBytes()));

    String fragment = "<name>Changed Name</name>";
    Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes()));

    Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild());

    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile("//people/person[2]/name");
    Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE);

    Node parentNode = nodeFound.getParentNode();
    parentNode.removeChild(nodeFound);
    parentNode.appendChild(injectedNode);

    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(domSource, result);
    System.out.println(result.getWriter().toString());
}

From source file:be.error.rpi.test.UdpTest.java

public static void main(String[] args) throws Exception {
    final InetAddress IPAddress = InetAddress.getByName("192.168.0.10");
    final DatagramSocket clientSocket = new DatagramSocket();

    new Thread() {
        @Override/*from w ww.  j a v  a 2  s .c om*/
        public void run() {
            try {
                while (true) {
                    String s = "0:0:0:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

    new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    String s = "1:1:1:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

}

From source file:md5.demo.MD5Demo.java

public static void main(String[] args) {
    String input = "123456";
    try {//from  ww  w.ja v a2 s  . c o  m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] output = messageDigest.digest(input.getBytes());
        String outString = Hex.encodeHexString(output);
        System.out.println(outString);

    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }

}

From source file:ch.windmobile.server.social.mongodb.util.AuthenticationServiceUtil.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    String email = args[0];//from w ww .ja  v a  2  s .c o  m
    String password = args[1];
    System.out.println(
            "Email: " + email + ", password: " + password + ", sha1:" + createSHA1(email, password.getBytes()));
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    String initial = "<root><param value=\"abc\"/><param value=\"bc\"/></root>";
    ByteArrayInputStream is = new ByteArrayInputStream(initial.getBytes());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);// w w w.j  a v a  2 s.  c  o m

    // Create the new xml fragment
    Text a = doc.createTextNode("asdf");
    Node p = doc.createElement("parameterDesc");
    p.appendChild(a);
    Node i = doc.createElement("insert");
    i.appendChild(p);
    Element r = doc.getDocumentElement();
    r.insertBefore(i, r.getFirstChild());
    r.normalize();

    // Format the xml for output
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    // initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);

    System.out.println(result.getWriter().toString());

}

From source file:ZipDemo.java

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; ++i) {
        String uncompressed = "";
        File f = new File(args[i]);

        if (f.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(f));

            String line = "";
            StringBuffer buffer = new StringBuffer();

            while ((line = br.readLine()) != null)
                buffer.append(line);//from   w ww.jav  a2  s.  co m

            br.close();
            uncompressed = buffer.toString();
        } else {
            uncompressed = args[i];
        }

        byte[] compressed = ZipDemo.compress(uncompressed);

        String compressedAsString = new String(compressed);

        byte[] bytesFromCompressedAsString = compressedAsString.getBytes();

        bytesFromCompressedAsString.equals(compressed);
        System.out.println(ZipDemo.uncompress(compressed));
        System.out.println(ZipDemo.uncompress(compressedAsString));
    }
}

From source file:com.yahoo.ads.pb.mttf.PistachiosMTTFTest.java

public static void main(String[] args) {
    PistachiosClient client;//from  w w w .j  av a  2  s . c o m
    try {
        client = new PistachiosClient();
    } catch (Exception e) {
        logger.info("error creating client", e);
        return;
    }
    Random rand = new Random();

    while (true) {
        try {
            long id = rand.nextLong();
            String value = InetAddress.getLocalHost().getHostName() + rand.nextInt();
            client.store(0, id, value.getBytes());
            for (int i = 0; i < 30; i++) {
                byte[] clientValue = client.lookup(0, id);
                String remoteValue = new String(clientValue);
                if (Arrays.equals(value.getBytes(), clientValue)
                        || !remoteValue.contains(InetAddress.getLocalHost().getHostName())) {
                    logger.debug("succeeded checking id {} value {}", id, value);
                } else {
                    logger.error("failed checking id {} value {} != {}", id, value, new String(clientValue));
                    System.exit(0);
                }
                Thread.sleep(100);
            }
        } catch (Exception e) {
            System.out.println("error testing" + e);
            System.exit(0);
        }
    }
}

From source file:core.Hash.java

/**
 * @param args the command line arguments
 */// ww  w  .j  av  a 2 s  . co  m
public static void main(String[] args) {

    MessageDigest md = null;
    String password = "password D:";
    try {

        //SHA-512
        md = MessageDigest.getInstance("SHA-512");
        md.update(password.getBytes());
        byte[] mb = md.digest();
        System.out.println(Hex.encodeHex(mb));
        //SHA-1
        md = MessageDigest.getInstance("SHA-1");
        md.update(password.getBytes());
        mb = md.digest();
        System.out.println(Hex.encodeHex(mb));
        //MD5
        md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        mb = md.digest();
        System.out.println(Hex.encodeHex(mb));

    } catch (NoSuchAlgorithmException e) {
        //Error
    }
}

From source file:com.lexmark.saperion.util.ClientMultipartFormPost.java

public static void main(String[] args) throws Exception {
    String userName = "amolugu";
    String password = "ecm";
    String authString = userName + ":" + password;
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*ww w .  j a v a2 s  .co  m*/
        HttpPost httpPost = new HttpPost("https://ecm-service.psft.co/ecms/documents");

        //http
        httpPost.addHeader("Authorization", "Basic " + authStringEnc);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("saTenant", "india");
        httpPost.addHeader("saLicense", "1");
        httpPost.addHeader("Content-Type", "application/octet-stream");

        FileBody bin = new FileBody(new File("C:\\Users\\Aditya.Molugu\\workspace\\RestClient\\Binaries.txt"));
        StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

        HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment)
                .build();

        //HttpBo
        httpPost.setEntity(reqEntity);

        System.out.println("executing request " + httpPost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpPost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
            }
            EntityUtils.consume(resEntity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.OracleObfuscation.java

public static void main(String[] args) throws Exception {
    OracleObfuscation x = new OracleObfuscation("$_12345&"); // just a
    // random/*www.ja  v a 2 s  . co  m*/
    // pick for
    // testing
    String text = CommonUtil.pad("BB", Constants.MAX_ANSWER_LENGTH); //args[0];

    byte[] encrypted = x.encrypt(text.getBytes());
    String encoded = new String(Hex.encodeHex(encrypted));
    System.out.println("Encrypted/Encoded: \"" + encoded + "\"");

    byte[] decoded = Hex.decodeHex(encoded.toCharArray());
    String decrypted = new String(x.decrypt(decoded));
    //      String decrypted = new String(x.decrypt(encrypted));
    System.out.println("Decoded/Decrypted: \"" + decrypted + "\"");
}