MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 9 as of 2016-09-16 09:48:52
  • Java

Java

Regular expressions

   1 /*
   2 javac TesteRe.java 
   3 java -cp . TesteRe 
   4 */
   5 /*
   6 gcj -C TesteRe.java  # compile with gcj, GNU compiler for Java
   7 gij -cp . TesteRe # run with gij, GNU interpreter for Java
   8 
   9 Linux executable
  10 gcj --main=TesteRe -g -o TesteRe TesteRe.java
  11 ldd TesteRe
  12 ./TesteRe
  13 */
  14 
  15 import java.util.regex.Pattern;
  16 import java.util.regex.Matcher;
  17 
  18 public class TesteRe{
  19 
  20     public static void main(String args[]){ 
  21         String values[]={"aa12.txt","a123sss.txt","bs11bb.txt","123aaaa.sql","a12.txt","aaaa12","20ghj","6657"};
  22         Pattern a = Pattern.compile("^(\\D+)(\\d+)(\\D+)$");
  23         Pattern b = Pattern.compile("^(\\d+)(\\D+)$");
  24         Pattern c = Pattern.compile("^(\\D+)(\\d+)$");
  25         Pattern d = Pattern.compile("^(\\d+)$");
  26 
  27         for(String item:values){
  28             Matcher ma = a.matcher(item);
  29             Matcher mb = b.matcher(item);
  30             Matcher mc = c.matcher(item);
  31             Matcher md = d.matcher(item);
  32 
  33            if(ma.matches()){ 
  34                int val = Integer.parseInt(ma.group(2)) + 1;
  35                System.out.println(String.format("A: mv %s %s%d%s",item,ma.group(1),val,ma.group(3)  ) );
  36            }
  37 
  38            if(mb.matches()){ 
  39                int val = Integer.parseInt(mb.group(1)) + 1;
  40                System.out.println(String.format("B: mv %s %d%s",item, val , mb.group(2)  ) );
  41            }
  42 
  43            if(mc.matches()){ 
  44                int val = Integer.parseInt(mc.group(2)) + 1;
  45                System.out.println(String.format("C: mv %s %s%d",item,mc.group(1),val   ) );
  46            }
  47 
  48            if(md.matches()){ 
  49                int val = Integer.parseInt(md.group(1)) + 1;
  50                System.out.println(String.format("D: mv %s %d",item, val  ) );
  51            }
  52 
  53         }
  54     }
  55 }

Server/client

Server

   1 import java.net.ServerSocket;
   2 import java.net.Socket;
   3 import java.lang.Thread;
   4 import java.util.Stack;
   5 
   6 public class SocketServer{
   7     public static void main(String []args){
   8         ServerSocket ss = null; 
   9         int nrThreads= Runtime.getRuntime().availableProcessors() * 2; 
  10         System.out.println("Nr cores:" + nrThreads);
  11         ConnHandler[] c = new ConnHandler[nrThreads];
  12         for(int i=0;i<nrThreads;i++){
  13             c[i]=new ConnHandler();
  14             c[i].start();
  15         }
  16         
  17         try{ 
  18             ss = new ServerSocket(1234); 
  19             ss.setReuseAddress(true);
  20         }
  21         catch(Exception ex){    }
  22         long count=0;
  23         while(true){
  24           try{
  25             Socket s = ss.accept(); 
  26             s.setSoLinger(true,0);
  27             int idx = (int)count%nrThreads;
  28             c[ idx  ].addSocket(s);
  29             count++;
  30             
  31           }
  32           catch(Exception ex){
  33               System.out.println(ex.getMessage());
  34           }
  35         }
  36     }
  37 }

ConnHandler

   1 import java.net.ServerSocket;
   2 import java.net.Socket;
   3 import java.lang.Thread;
   4 import java.util.Stack;
   5 
   6 public class ConnHandler extends Thread{
   7     private byte[] rx;
   8     private Stack stack;
   9     long tid;
  10 
  11     public ConnHandler(){
  12         this.rx= new byte[1024];
  13         this.stack = new Stack();
  14     }
  15 
  16     public void addSocket(Socket s){
  17         this.stack.push(s);
  18     }
  19 
  20     public void run(){
  21         this.tid = Thread.currentThread().getId();
  22 
  23         while(true){
  24             if( this.stack.empty() == false){
  25                 Socket s = (Socket)this.stack.pop();
  26                 try{
  27                     if( s.getInputStream().available() > 0 ){
  28                         int readedBytes = s.getInputStream().read( rx );
  29                          System.out.println("ThreadId:"+tid  + " ReadedBytes:"+ readedBytes + 
  30 " StackSize:" + this.stack.size() );
  31                     }
  32                     s.close();
  33                 }
  34                 catch(Exception ex){
  35                 } 
  36             }
  37             else{ try{ Thread.sleep(500); }catch(Exception ex){} }
  38         }
  39     }
  40 }

Client (python)

   1 import threading
   2 import time
   3 import socket
   4 
   5 class Client (threading.Thread):
   6     def __init__(self):
   7         threading.Thread.__init__(self) #required
   8 
   9     def run(self):
  10         for x in range(1,100):
  11             HOST = 'localhost'
  12             PORT = 1234
  13             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14             print('connecting...')
  15             s.connect((HOST, PORT))
  16             print('sending config...')
  17             s.send('test\r\n')
  18             s.close()
  19             print('complete')
  20 
  21 if __name__=='__main__':
  22     clients=[]
  23     for x in range(1,100):
  24         clients.append( Client() )
  25     for c in clients:
  26         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

   1 java.lang.refelct.Field f = ClassX.class.getDeclaredField("fieldx");
   2 f.setAccessible(true);
   3 FieldType ft = f.get(objectWithPrivateField);

Generate Javadoc using command line

  • dir /s /b *.java > javafiles.txt

  • javadoc -encoding UTF-8 -d docs @javafiles.txt
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01