Home » Core Java » lang » Page 7

lang

Convert String to byte array ASCII encoding

This is an example of how to convert a String to byte array with ASCII encoding. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Converting a String to byte array with ASCII encoding implies that you should: Create a String. Create a byte array with size ...

Read More »

Convert String to byte array UTF encoding

public static byte[] stringToBytesUTFCustom(String str) { byte[] b = new byte[str.length() << 1]; for(int i = 0; i < str.length(); i++) { char strChar = str.charAt(i); int bpos = i << 1; b[bpos] = (byte) ((strChar&0xFF00)>>8); b[bpos + 1] = (byte) (strChar&0x00FF); } return b; } public static String bytesToStringUTFCustom(byte[] bytes) { char[] buffer = new char[bytes.length >> 1]; for(int ...

Read More »

Code injection with Java Proxy example

I was using JDBC PreparedStatement’s batch updates to modify a lot of data in a database and the processing was failing for one of the batch updates because of integrity constraint violation. The exception didn’t contain enough information to find out which data caused the failure and so I’ve created a dynamic proxy for the PreparedStatement that remembered values passed ...

Read More »

MethodHandle example

public class MethodAccessExampleWithArgs { private final int i; public MethodAccessExampleWithArgs(int i_) { i = i_; } private void bar(int j, String msg) { System.out.println("Private Method 'bar' successfully accessed : " + i + ", " + j + " : " + msg + "!"); } // Using Reflection public static Method makeMethod() { Method meth = null; try { ...

Read More »