Beispiel #1
0
  public void openFile(String filename) {
    try {
      FileInputStream input = new FileInputStream(filename);
      char x;

      for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
          x = (char) input.read();

          if (x == 'P') {
            puzzleBox[i][j].setText(String.valueOf((char) input.read()));
            puzzleBox[i][j].setEnabled(false);
          } else {
            x = (char) input.read();

            if (x == 'X') {
              puzzleBox[i][j].setText("");
              puzzleBox[i][j].setEnabled(true);
            } else {
              puzzleBox[i][j].setText(String.valueOf(x));
              puzzleBox[i][j].setEnabled(true);
            }
          }
        }

        x = (char) input.read();
        x = (char) input.read();
      }

      input.close();
    } catch (IOException ioException) {
      JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
  /**
   * Loads the bytes in a file.
   *
   * @param name the file name
   * @return an array with the bytes in the file
   */
  public byte[] loadBytes(String name) throws IOException {
    FileInputStream in = null;

    in = new FileInputStream(name);
    try {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      int ch;
      while ((ch = in.read()) != -1) buffer.write(ch);
      return buffer.toByteArray();
    } finally {
      in.close();
    }
  }
Beispiel #3
0
  /** Procedure to set value for the code-swap strings */
  void getCodeSwapString() {
    File inputFile = new File(fileStr1);
    FileInputStream input = null;
    try {
      input = new FileInputStream(inputFile);
    } catch (FileNotFoundException fe) {
      System.err.println(fileStr1 + " not found. " + fe);
      // System.exit(-1);;
    }
    byte bt[] = new byte[(int) inputFile.length()];
    try {
      input.read(bt);
      textStr1 = new String(bt);
      input.close();
    } catch (IOException ie) {
      System.err.println("Can't read from the input stream " + ie);
      // System.exit(-1);;
    }

    inputFile = new File(fileStr2);
    try {
      input = new FileInputStream(inputFile);
    } catch (FileNotFoundException fe) {
      System.err.println(fileStr2 + " not found. " + fe);
      // System.exit(-1);;
    }
    try {
      bt = new byte[(int) inputFile.length()];
      input.read(bt);
      textStr2 = new String(bt);
      input.close();
    } catch (IOException ie) {
      System.err.println("Can't read from the input stream " + ie);
      // System.exit(-1);;
    }
  }
Beispiel #4
0
  public Class loadNewClass(String javaFile, String packageName) {
    Class myClass = null;

    String className = javaFile.replace(".java", ".class");

    File inpFile = new File(className);
    int idx = className.lastIndexOf(File.separatorChar);
    String javaName = className.substring(idx + 1, className.indexOf("."));
    int size = (int) inpFile.length();
    byte[] ba = new byte[size];
    try {
      FileInputStream fis = new FileInputStream(className);

      // read the entry
      int bytes_read = 0;
      while (bytes_read != size) {
        int r = fis.read(ba, bytes_read, size - bytes_read);
        if (r < 0) break;
        bytes_read += r;
      }
      if (bytes_read != size) throw new IOException("cannot read entry");
    } catch (FileNotFoundException fnfExc) {
      System.out.println("File : " + className + " not found");
      fnfExc.printStackTrace();
    } catch (IOException ioExc) {
      System.out.println("IO Exception in JavaCompile trying to read class: ");
      ioExc.printStackTrace();
    }

    try {
      String packageAppendedFileName = "";
      if (packageName.isEmpty()) packageAppendedFileName = javaName;
      else packageAppendedFileName = packageName + "." + javaName;
      packageAppendedFileName.replace(File.separatorChar, '.');
      myClass = defineClass(packageAppendedFileName, ba, 0, size);
      // SOS-StergAutoCompletionScalaSci.upDateAutoCompletion(myClass);
      String userClassName = myClass.getName();
      JOptionPane.showMessageDialog(null, "Class " + userClassName + " loaded successfully !");
    } catch (ClassFormatError exc) {
      System.out.println("error defining class " + inpFile);
      exc.printStackTrace();
    } catch (Exception ex) {
      System.out.println("some error defining class " + inpFile);
      ex.printStackTrace();
    }
    return myClass;
  }
Beispiel #5
0
  protected byte[] getClassData(String directory, String name) {
    String classFile = directory + "/" + name.replace('.', '/') + ".class";

    int classSize = (int) new File(classFile).length();
    byte[] buf = new byte[classSize];

    try {
      FileInputStream filein = new FileInputStream(classFile);
      classSize = filein.read(buf);
      filein.close();
    } catch (FileNotFoundException e) {
      return null;
    } catch (IOException e) {
      return null;
    }
    return buf;
  }
Beispiel #6
0
 private void openBin() {
   fileChooser.resetChoosableFileFilters();
   fileChooser.addChoosableFileFilter(binFilter);
   fileChooser.setFileFilter(binFilter);
   if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
     try {
       FileInputStream inbinf = new FileInputStream(fileChooser.getSelectedFile());
       int len = inbinf.available();
       if (len % 2 == 1) throw new IOException(String.format("Odd file size (0x%x)\n", len));
       len /= 2;
       if (len > 0x10000) throw new IOException(String.format("Too large file (0x%x)\n", len));
       binary = new char[len];
       for (int i = 0; i < len; i++) {
         int lo = inbinf.read();
         int hi = inbinf.read();
         if (lo == -1 || hi == -1) throw new IOException("Unable to read\n");
         binary[i] = (char) ((hi << 8) | lo);
       }
       asmMap = new AsmMap();
       Disassembler dasm = new Disassembler();
       dasm.init(binary);
       // TODO attach asmmap
       StringBuilder sb = new StringBuilder();
       while (dasm.getAddress() < binary.length) {
         int addr = dasm.getAddress();
         sb.append(String.format("%-26s ; [%04x] =", dasm.next(true), addr));
         int addr2 = dasm.getAddress();
         while (addr < addr2) {
           char i = binary[addr++];
           sb.append(
               String.format(" %04x '%s'", (int) i, (i >= 0x20 && i < 0x7f) ? (char) i : '.'));
         }
         sb.append("\n");
       }
       srcBreakpoints.clear();
       sourceRowHeader.breakpointsChanged();
       sourceTextarea.setText(sb.toString());
     } catch (IOException e1) {
       JOptionPane.showMessageDialog(
           frame, "Unable to open file: %s" + e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
       e1.printStackTrace();
     }
   }
 }
  public static void main(String[] arg) {
    if (arg.length != 2) {
      System.err.println("usage: java ScpTo file1 user@remotehost:file2");
      System.exit(-1);
    }

    FileInputStream fis = null;
    try {

      String lfile = arg[0];
      String user = arg[1].substring(0, arg[1].indexOf('@'));
      arg[1] = arg[1].substring(arg[1].indexOf('@') + 1);
      String host = arg[1].substring(0, arg[1].indexOf(':'));
      String rfile = arg[1].substring(arg[1].indexOf(':') + 1);

      JSch jsch = new JSch();
      Session session = jsch.getSession(user, host, 22);

      // username and password will be given via UserInfo interface.
      UserInfo ui = new MyUserInfo();
      session.setUserInfo(ui);
      session.connect();

      boolean ptimestamp = true;

      // exec 'scp -t rfile' remotely
      String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rfile;
      Channel channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);

      // get I/O streams for remote scp
      OutputStream out = channel.getOutputStream();
      InputStream in = channel.getInputStream();

      channel.connect();

      if (checkAck(in) != 0) {
        System.exit(0);
      }

      File _lfile = new File(lfile);

      if (ptimestamp) {
        command = "T " + (_lfile.lastModified() / 1000) + " 0";
        // The access time should be sent here,
        // but it is not accessible with JavaAPI ;-<
        command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
        out.write(command.getBytes());
        out.flush();
        if (checkAck(in) != 0) {
          System.exit(0);
        }
      }

      // send "C0644 filesize filename", where filename should not include '/'
      long filesize = _lfile.length();
      command = "C0644 " + filesize + " ";
      if (lfile.lastIndexOf('/') > 0) {
        command += lfile.substring(lfile.lastIndexOf('/') + 1);
      } else {
        command += lfile;
      }
      command += "\n";
      out.write(command.getBytes());
      out.flush();
      if (checkAck(in) != 0) {
        System.exit(0);
      }

      // send a content of lfile
      fis = new FileInputStream(lfile);
      byte[] buf = new byte[1024];
      while (true) {
        int len = fis.read(buf, 0, buf.length);
        if (len <= 0) break;
        out.write(buf, 0, len); // out.flush();
      }
      fis.close();
      fis = null;
      // send '\0'
      buf[0] = 0;
      out.write(buf, 0, 1);
      out.flush();
      if (checkAck(in) != 0) {
        System.exit(0);
      }
      out.close();

      channel.disconnect();
      session.disconnect();

      System.exit(0);
    } catch (Exception e) {
      System.out.println(e);
      try {
        if (fis != null) fis.close();
      } catch (Exception ee) {
      }
    }
  }