Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

ineya

By: a guest on Jul 17th, 2008  |  syntax: Java  |  size: 1.77 KB  |  hits: 111  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. // dsh - Data shell
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11.  
  12. public class DataShell {
  13.  
  14.         public static class F extends Thread {
  15.                 public void run() {
  16.                         try {
  17.                                 sleep(100);
  18.                                 System.out.flush();
  19.                         } catch (InterruptedException e) {
  20.                                 // TODO Auto-generated catch block
  21.                                
  22.                                 e.printStackTrace();
  23.                         }
  24.                        
  25.                 }
  26.         }
  27.        
  28.         public static void copyFileToStream(String filename, OutputStream o) throws IOException {
  29.                 FileInputStream fis = new FileInputStream(filename);
  30.                                
  31.                 int c=0;
  32.                 while ((c=fis.read()) != -1) {
  33.                         o.write(c);
  34.                 }
  35.                
  36.                 fis.close();
  37.         }
  38.        
  39.         /**
  40.          * @param args
  41.          */
  42.         public static void main(String[] args) {
  43.                 InputStreamReader isr = new InputStreamReader(System.in);
  44.                 BufferedReader br = new BufferedReader(isr);
  45.                 String line;
  46.                
  47.                 try {
  48.                         new F().start();
  49.                        
  50.                         while ((line = br.readLine()) != null) {
  51.                                
  52.                                 if (line.startsWith("q")) {
  53.                                         break;
  54.                                 } else  if (line.startsWith("a")) {
  55.                                         line = line.substring(1);
  56.                                         System.out.write(line.getBytes());
  57.                                 } else if (line.startsWith("h")) {
  58.                                     Pattern p = Pattern.compile("0x([0-9a-fA-F]{1,2})");
  59.                                     Matcher m = p.matcher(line);
  60.                                    
  61.                                     while (m.find()) {
  62.                                         String hexNumber = m.group(1);
  63.                                         byte b=Byte.parseByte(hexNumber, 16);
  64.                                         System.out.write(b);
  65.                                        
  66.                                     }
  67.                                 } else if (line.startsWith("f")) {
  68.                                         line = line.substring(1);
  69.                                         copyFileToStream(line, System.out);
  70.                                 }
  71.                                
  72.                                 //System.out.write(line.getBytes());
  73.                                 System.out.flush();
  74.                         }
  75.                 } catch (IOException e) {
  76.                         e.printStackTrace();
  77.                 }
  78.         }
  79.  
  80. }