Esempio n. 1
0
  private void saveData(DataOutputStream sv, String directory) {
    try {
      // 8 bit registers
      sv.write(registers[a]);
      sv.write(registers[b]);
      sv.write(registers[c]);
      sv.write(registers[d]);
      sv.write(registers[e]);
      sv.write(f);

      // 16 bit registers
      sv.writeInt(sp);
      sv.writeInt(pc);
      sv.writeInt(hl);

      sv.writeInt(gbcRamBank);

      // write ram 8Kb
      sv.write(mainRam);

      // write oam (used mainly for registers) 256 bytes
      sv.write(oam);

    } catch (IOException e) {
      System.out.println("Dmgcpu.saveState.saveData: Could not write to file " + directory);
      System.out.println("Error Message: " + e.getMessage());
      System.exit(-1);
    }
  }
Esempio n. 2
0
  /** Implement the run() method for the thread */
  public void run() {
    try {
      // Create data input and output streams
      DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream());
      DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream());
      DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream());
      DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream());

      // Write anything to notify player 1 to start
      // This is just to let player 1 know to start
      toPlayer1.writeInt(1);

      // Continuously serve the players and determine and report
      // the game status to the players
      while (true) {
        // Receive a move from player 1
        int row = fromPlayer1.readInt();
        int column = fromPlayer1.readInt();
        int yCoordinate = game.insert("[X]", column);

        // Check if Player 1 wins
        if (game.isWinner(column, yCoordinate, "[X]")) { // if a  winner is found
          toPlayer1.writeInt(PLAYER1_WON);
          toPlayer2.writeInt(PLAYER1_WON);
          sendMove(toPlayer2, row, column);
          break; // Break the loop
        } else {
          toPlayer2.writeInt(CONTINUE);
          sendMove(toPlayer2, row, column);
        }
        // Receive a move from Player 2
        row = fromPlayer2.readInt();
        column = fromPlayer2.readInt();
        yCoordinate = game.insert("[O]", column);
        // Check if Player 2 wins
        if (game.isWinner(column, yCoordinate, "[O]")) { // if a  winner is found
          toPlayer1.writeInt(PLAYER2_WON);
          toPlayer2.writeInt(PLAYER2_WON);
          sendMove(toPlayer1, row, column);
          break; // Break the loop
        } else if (game.boardIsFull()) { // Check if all cells are filled
          toPlayer1.writeInt(DRAW);
          toPlayer2.writeInt(DRAW);
          sendMove(toPlayer1, row, column);
          break;
        } else {
          // Notify player 2 to take the turn
          toPlayer1.writeInt(CONTINUE);
          // Send player 1's selected row and column to player 2
          sendMove(toPlayer1, row, column);
        }
      }
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
Esempio n. 3
0
 /**
  * \ Draw saved State (color dots on panel) on WhiteBoard
  *
  * @param outstream
  * @throws IOException
  */
 public void writeState(OutputStream outstream) throws IOException {
   if (state == null) return;
   synchronized (state) {
     DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(outstream));
     // DataOutputStream dos=new DataOutputStream(outstream);
     dos.writeInt(state.size());
     for (Map.Entry<Point, Color> entry : state.entrySet()) {
       Point point = entry.getKey();
       Color col = entry.getValue();
       dos.writeInt(point.x);
       dos.writeInt(point.x);
       dos.writeInt(col.getRGB());
     }
     dos.flush();
     System.out.println("wrote " + state.size() + " elements");
   }
 }
  public void guardarVentaIngreso() {
    try {
      FileOutputStream fos = new FileOutputStream("ingreso.dat");
      DataOutputStream dos = new DataOutputStream(fos);
      dos.writeDouble(sistema.getEmpresa().getIngreso());
      dos.writeInt(sistema.getEmpresa().getNumVentas());
      dos.close();
    } catch (IOException ex) {

    }
  }
 private final int getuid(String s) {
   try {
     File file = new File(s + "uid.dat");
     if (!file.exists() || file.length() < 4L) {
       DataOutputStream dataoutputstream =
           new DataOutputStream(new FileOutputStream(s + "uid.dat"));
       dataoutputstream.writeInt((int) (Math.random() * 99999999D));
       dataoutputstream.close();
     }
   } catch (Exception _ex) {
   }
   try {
     DataInputStream datainputstream = new DataInputStream(new FileInputStream(s + "uid.dat"));
     int i = datainputstream.readInt();
     datainputstream.close();
     return i + 1;
   } catch (Exception _ex) {
     return 0;
   }
 }
Esempio n. 6
0
  public void run() {
    String objRouter = applet.getParameter("name");
    // No Internationalisation
    try {
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      DataOutputStream outp = new DataOutputStream(byteStream);
      outp.writeInt(GenericConstants.ROUTER_PROPERTIES);
      outp.writeUTF(objRouter);
      outp.flush();

      byte[] bytes = byteStream.toByteArray();
      outp.close();
      byteStream.reset();
      byteStream.close();
      byte[] data = GenericSession.getInstance().syncSend(bytes);
      if (data != null) {
        DataInputStream inp = new DataInputStream(new ByteArrayInputStream(data));
        int reqId = inp.readInt();
        if (reqId == GenericConstants.ROUTER_PROPERTIES) {
          int length = inp.readInt();
          byte serverData[] = new byte[length];
          inp.readFully(serverData);
          routerobject = NmsClientUtil.deSerializeVector(serverData);
        }

        init();
        refresh();
        super.setVisible(true);
      }
      /*init();
      refresh();
      super.setVisible(true);*/

      else close();

    } catch (Exception e) {
      // NmsClientUtil.err(NmsClientUtil.getFrame(app),"IO Error sending request to server.
      // "+e);//No Internationalisation
    }
  }
Esempio n. 7
0
 /* (non-Javadoc)
  * @see edu.mit.csail.cgs.utils.Saveable#save(java.io.DataOutputStream)
  */
 public void save(DataOutputStream dos) throws IOException {
   dos.writeInt(2);
   super.save(dos);
 }
Esempio n. 8
0
    @Override
    public void run() {

      try {
        // Create data input and output streams
        DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
        DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

        // Set up a byte buffer to capture the file from the client
        byte[] buffer = new byte[BUFFER_SIZE];
        OutputStream outputStream = null;
        String fileName = "";
        boolean createFile = true;
        int bytesReceived = 0;
        long totalBytesReceived = 0;
        long fileSize = 0;

        // Continuously serve the client
        while (true) {
          bytesReceived = inputFromClient.read(buffer);

          if (bytesReceived
              > 0) { // Get the file transmission header from the initial client packet
            String transmitHeader = new String(buffer, 0, bytesReceived);
            // transmitHeader = transmitHeader.substring(0, bytesReceived).trim().;
            String[] header = transmitHeader.split(HEADER_DEL);
            fileSize = Long.parseLong(header[0]);
            fileName = header[1];

            // Send receipt acknowledgment back to the client. Just send back the number of bytes
            // received.
            outputToClient.writeInt(bytesReceived);

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }

          // Wait for client to send bytes
          while ((bytesReceived = inputFromClient.read(buffer)) != -1) {
            if (inputFromClient.available() > 0) {
              if (createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              // Write bytes to file
              outputStream.write(buffer, 0, bytesReceived);
            } else { // We get here if no more data is available, but some bytes were already
              // received

              // If bytes were received and the file wasn't already created,
              // it means that the file was smaller than our buffer size.
              // Create the file...
              if (bytesReceived > 0
                  && createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              if (outputStream != null) {
                if (bytesReceived > 0) { // Write remaining bytes to file, if any
                  outputStream.write(buffer, 0, bytesReceived);
                }

                outputStream.flush();
                outputStream.close();
                textArea.append("Received file successfully. Saved as " + fileName + "\n");

                // Return success to client.
                outputToClient.writeInt(0);
              }

              // Reset creation flag
              createFile = true;
              break;
            }

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }
        }
      } catch (IOException e) {
        System.err.println(e);
      }
    }
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbSaveLayer) {
      try {
        FileOutputStream fout = new FileOutputStream(jtfCengMing.getText() + ".wyf");
        ObjectOutputStream oout = new ObjectOutputStream(fout);
        oout.writeObject(itemArray);
        oout.close();
        fout.close();
      } catch (Exception ea) {
        ea.printStackTrace();
      }
    } else if (e.getSource() == jbLoadLayer) {
      try {
        FileInputStream fin = new FileInputStream(jtfCengMing.getText() + ".wyf");
        ObjectInputStream oin = new ObjectInputStream(fin);
        itemArray = (Item[][]) oin.readObject();

        oin.close();
        fin.close();
        this.flush();
      } catch (Exception ea) {
        ea.printStackTrace();
      }
      lvp.repaint();
    } else if (e.getSource() == jbLoadAll) { // 全部铺上当前选中

      for (int row = 0; row < 40; row++) {
        for (int col = 0; col < 60; col++) {
          Item item = ((Item) (jl.getSelectedValue())).clone();
          itemArray[row][col] = item;
          if (item != null) {
            item.setPosition(col, row);
          }
        }
      }

      lvp.repaint();
    } else if (e.getSource() == jbCreate) { // 生成源代码

      try {
        FileOutputStream fout = null;
        DataOutputStream dout = null;
        fout = new FileOutputStream("maps.so");
        dout = new DataOutputStream(fout);
        int totalBlocks = 0;

        for (int i = 0; i < 40; i++) {
          for (int j = 0; j < 60; j++) {
            Item item = itemArray[i][j];
            if (item != null) {
              totalBlocks++;
            }
          }
        }
        System.out.println("totalBlocks=" + totalBlocks);

        // 写入不空块的数量
        dout.writeInt(totalBlocks);

        for (int i = 0; i < 40; i++) {
          for (int j = 0; j < 60; j++) {
            Item item = itemArray[i][j];
            if (item != null) {
              int w = item.w; // 元素的图片宽度
              int h = item.h; // 元素的图片高度
              int col = item.col; // 元素的地图列
              int row = item.row; // 元素的地图行
              int pCol = item.pCol; // 元素的占位列
              int pRow = item.pRow; // 元素的占位行
              String leiMing = item.leiMing; // 类名

              int[][] notIn = item.notIn; // 不可通过
              int[][] keYu = item.keYu; // 可遇矩阵

              // 计算图片下标
              int outBitmapInxex = 0;
              if (leiMing.equals("Grass")) {
                outBitmapInxex = 0;
              } else if (leiMing.equals("XiaoHua1")) {
                outBitmapInxex = 1;
              } else if (leiMing.equals("MuZhuang")) {
                outBitmapInxex = 2;
              } else if (leiMing.equals("XiaoHua2")) {
                outBitmapInxex = 3;
              } else if (leiMing.equals("Road")) {
                outBitmapInxex = 4;
              } else if (leiMing.equals("Jing")) {
                outBitmapInxex = 5;
              }

              dout.writeByte(outBitmapInxex); // 记录图片下标
              dout.writeByte(0); // 记录可遇标志 0-不可遇 底层都不可遇
              dout.writeByte(w); // 图片宽度
              dout.writeByte(h); // 图片高度
              dout.writeByte(col); // 总列数
              dout.writeByte(row); // 总行数
              dout.writeByte(pCol); // 占位列
              dout.writeByte(pRow); // 占位行

              int bktgCount = notIn.length; // 不可通过点的数量
              dout.writeByte(bktgCount); // 写入不可通过点的数量

              for (int k = 0; k < bktgCount; k++) {
                dout.writeByte(notIn[k][0]);
                dout.writeByte(notIn[k][1]);
              }
            }
          }
        }

        dout.close();
        fout.close();

      } catch (Exception ea) {
        ea.printStackTrace();
      }
    }
  }
Esempio n. 10
0
 /** Send the move to other player */
 private void sendMove(DataOutputStream out, int row, int column) throws IOException {
   out.writeInt(row); // Send row index
   out.writeInt(column); // Send column index
 }