= Java = == Regular expressions == {{{#!Java /* javac TesteRe.java java -cp . TesteRe */ /* gcj -C TesteRe.java # compile with gcj, GNU compiler for Java gij -cp . TesteRe # run with gij, GNU interpreter for Java Linux executable gcj --main=TesteRe -g -o TesteRe TesteRe.java ldd TesteRe ./TesteRe */ import java.util.regex.Pattern; import java.util.regex.Matcher; public class TesteRe{ public static void main(String args[]){ String values[]={"aa12.txt","a123sss.txt","bs11bb.txt","123aaaa.sql","a12.txt","aaaa12","20ghj","6657"}; Pattern a = Pattern.compile("^(\\D+)(\\d+)(\\D+)$"); Pattern b = Pattern.compile("^(\\d+)(\\D+)$"); Pattern c = Pattern.compile("^(\\D+)(\\d+)$"); Pattern d = Pattern.compile("^(\\d+)$"); for(String item:values){ Matcher ma = a.matcher(item); Matcher mb = b.matcher(item); Matcher mc = c.matcher(item); Matcher md = d.matcher(item); if(ma.matches()){ int val = Integer.parseInt(ma.group(2)) + 1; System.out.println(String.format("A: mv %s %s%d%s",item,ma.group(1),val,ma.group(3) ) ); } if(mb.matches()){ int val = Integer.parseInt(mb.group(1)) + 1; System.out.println(String.format("B: mv %s %d%s",item, val , mb.group(2) ) ); } if(mc.matches()){ int val = Integer.parseInt(mc.group(2)) + 1; System.out.println(String.format("C: mv %s %s%d",item,mc.group(1),val ) ); } if(md.matches()){ int val = Integer.parseInt(md.group(1)) + 1; System.out.println(String.format("D: mv %s %d",item, val ) ); } } } } }}} == Server/client == Server {{{#!java import java.net.ServerSocket; import java.net.Socket; import java.lang.Thread; import java.util.Stack; public class SocketServer{ public static void main(String []args){ ServerSocket ss = null; int nrThreads= Runtime.getRuntime().availableProcessors() * 2; System.out.println("Nr cores:" + nrThreads); ConnHandler[] c = new ConnHandler[nrThreads]; for(int i=0;i 0 ){ int readedBytes = s.getInputStream().read( rx ); System.out.println("ThreadId:"+tid + " ReadedBytes:"+ readedBytes + " StackSize:" + this.stack.size() ); } s.close(); } catch(Exception ex){ } } else{ try{ Thread.sleep(500); }catch(Exception ex){} } } } } }}} Client (python) {{{#!python import threading import time import socket class Client (threading.Thread): def __init__(self): threading.Thread.__init__(self) #required def run(self): for x in range(1,100): HOST = 'localhost' PORT = 1234 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('connecting...') s.connect((HOST, PORT)) print('sending config...') s.send('test\r\n') s.close() print('complete') if __name__=='__main__': clients=[] for x in range(1,100): clients.append( Client() ) for c in clients: c.start() }}} == JAVA_TOOL_OPTIONS == Since the command-line cannot always be accessed or modified, for example in embedded VMs or simply VMs launched deep within scripts, a JAVA_TOOL_OPTIONS variable is provided so that agents may be launched in these cases. * set JAVA_TOOL_OPTIONS=-Dfile.encoding=utf8 * export JAVA_TOOL_OPTIONS=-Dfile.encoding=utf8 http://docs.oracle.com/javase/7/docs/platform/jvmti/jvmti.html#tooloptions == Access private field via reflection == * http://stackoverflow.com/questions/1555658/is-it-possible-in-java-to-access-private-fields-via-reflection {{{#!highlight java java.lang.refelct.Field f = ClassX.class.getDeclaredField("fieldx"); f.setAccessible(true); FieldType ft = f.get(objectWithPrivateField); }}} == Generate Javadoc using command line == * dir /s /b *.java > javafiles.txt * javadoc -encoding UTF-8 -d docs @javafiles.txt To add an overview file add -overview overview.html == Java keystore public private keys == {{{ keytool -genkey -alias test1rsa -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore1.p12 -validity 3650 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: What is the name of your organizational unit? [Unknown]: What is the name of your organization? [Unknown]: What is the name of your City or Locality? [Unknown]: What is the name of your State or Province? [Unknown]: What is the two-letter country code for this unit? [Unknown]: Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? [no]: yes keytool -list -v -keystore keystore1.p12 -storetype pkcs12 }}} {{{#!highlight java /* javac keypair.java java -classpath . keypair */ import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Key; import java.security.cert.Certificate; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.security.spec.X509EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.EncodedKeySpec; import java.io.FileInputStream; import java.security.KeyFactory; public class keypair{ public static final String X509 = "X.509"; public static final String RSA = "RSA"; public static final String PKCS8 = "PKCS#8"; public static final String PKCS12 = "PKCS12"; private static Key loadKey(String filename,String format,String algorithm){ Key ret=null; try{ FileInputStream keyfis = new FileInputStream( filename ); byte[] data = new byte[keyfis.available()]; keyfis.read(data); keyfis.close(); EncodedKeySpec spec = null; KeyFactory keyFactory = KeyFactory.getInstance(algorithm); if(PKCS8.equals(format)){ spec = new PKCS8EncodedKeySpec(data); ret = keyFactory.generatePrivate(spec); System.out.println(String.format("Loading %s in format %s",filename , ret.getFormat() ) ); } if(X509.equals(format)){ spec = new X509EncodedKeySpec(data); ret = keyFactory.generatePublic(spec); System.out.println(String.format("Loading %s in format %s",filename , ret.getFormat() ) ); } } catch(Exception ex){ ex.printStackTrace(); } return ret; } private static void saveKey(Key key,String filename){ try{ System.out.println(String.format("Saving %s in format %s",filename , key.getFormat() ) ); byte[] encodedKey = key.getEncoded(); FileOutputStream keyfos = new FileOutputStream( filename ); keyfos.write(encodedKey); keyfos.close(); } catch(Exception ex){ ex.printStackTrace(); } } private static byte[] encrypt(Key key,String payload){ byte[] res=null; try{ Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, key); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(baos, cipher); byte[] b = payload.getBytes(); System.out.println( b.length ); cos.write(b); cos.flush(); cos.close(); res = baos.toByteArray(); System.out.println("Res size: "+ res.length ); for(int i=0;i