Advertisement
fbulterman

MachineFactory

Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package pad.ijvm;
  2.  
  3. import pad.ijvm.interfaces.IJVMInterface;
  4.  
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9.  
  10. public class MachineFactory {
  11.  
  12. public static IJVMInterface createIJVMInstance(File binary) throws IOException {
  13. // Create new machine instance here and return it.
  14. try{
  15. //File file = new File("/path/to/file.txt");
  16.  
  17. byte[] bytes = new byte[(int) binary.length()];
  18. FileInputStream fileInputStream = new FileInputStream(binary);
  19. fileInputStream.read(bytes);
  20. fileInputStream.close();
  21. ReadDoc processBinary = new ReadDoc(bytes);
  22. } catch (FileNotFoundException e){
  23. System.err.printf("%s\n", e.getMessage());
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. return null;
  29. }
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36. ----------------------------------------------------ReadDoc-----------------------------
  37. package pad.ijvm;
  38. import java.lang.System;
  39.  
  40. public class ReadDoc {
  41.  
  42. static final int BYTE_CONSTANT=4;
  43. int sizeOfText;
  44.  
  45. byte[] binary;
  46. byte[] constantAndText;
  47. int constantSize;
  48. byte[] text;
  49.  
  50.  
  51. ReadDoc(byte[] bytes){
  52. binary = new byte[(int) bytes.length];
  53. System.arraycopy(bytes, 0, binary, 0, bytes.length);
  54.  
  55. }
  56.  
  57. void getInfo(){
  58. constantAndText = new byte[((binary.length)-BYTE_CONSTANT)];
  59. System.arraycopy(binary, BYTE_CONSTANT, constantAndText, 0, constantAndText.length);
  60. readBlock();
  61. }
  62.  
  63. void readBlock(){
  64. constantSize= ((constantAndText[0] & 0xFF) << 24) |
  65. ((constantAndText[1] & 0xFF) << 16) |
  66. ((constantAndText[2] & 0xFF) << 8) |
  67. (constantAndText[3] & 0xFF);
  68. constantSize*=4; //every constant has 4 bytes
  69.  
  70.  
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement