예제 #1
0
  private void printResultsData(BattleCompletedEvent event) {
    // Do not print out if no result file has been specified and the GUI is enabled
    if ((setup.resultsFilename == null
        && (!setup.exitOnComplete || windowManager.isGUIEnabled()))) {
      return;
    }

    PrintStream out = null;
    FileOutputStream fos = null;

    try {
      if (setup.resultsFilename == null) {
        out = Logger.realOut;
      } else {
        File f = new File(setup.resultsFilename);

        try {
          fos = new FileOutputStream(f);
          out = new PrintStream(fos);
        } catch (IOException e) {
          Logger.logError(e);
        }
      }
      if (out != null) {
        BattleResultsTableModel resultsTable =
            new BattleResultsTableModel(
                event.getSortedResults(), event.getBattleRules().getNumRounds());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        resultsTable.print(new PrintStream(baos));
        out.append(StringUtil.toBasicLatin(baos.toString()));
      }
    } finally {
      FileUtil.cleanupStream(out);
      FileUtil.cleanupStream(fos);
    }
  }
예제 #2
0
  public void cleanupStreams() {
    FileUtil.cleanupStream(objectWriteStream);
    objectWriteStream = null;
    FileUtil.cleanupStream(bufferedWriteStream);
    bufferedWriteStream = null;
    FileUtil.cleanupStream(fileWriteStream);
    fileWriteStream = null;

    FileUtil.cleanupStream(objectReadStream);
    objectReadStream = null;
    FileUtil.cleanupStream(bufferedReadStream);
    bufferedReadStream = null;
    FileUtil.cleanupStream(fileReadStream);
    fileReadStream = null;
  }
예제 #3
0
  public void saveRecord(
      String recordFilename, BattleRecordFormat format, SerializableOptions options) {
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ZipOutputStream zos = null;
    ObjectOutputStream oos = null;
    OutputStreamWriter osw = null;
    XmlWriter xwr = null;

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    ObjectInputStream ois = null;

    final boolean isbin =
        format == BattleRecordFormat.BINARY || format == BattleRecordFormat.BINARY_ZIP;
    final boolean isxml = format == BattleRecordFormat.XML || format == BattleRecordFormat.XML_ZIP;
    Calendar calendar = Calendar.getInstance();

    try {
      fos = new FileOutputStream(recordFilename);
      bos = new BufferedOutputStream(fos, 1024 * 1024);

      if (format == BattleRecordFormat.BINARY) {
        oos = new ObjectOutputStream(bos);
      } else if (format == BattleRecordFormat.BINARY_ZIP) {
        zos = new ZipOutputStream(bos);
        zos.putNextEntry(new ZipEntry(dateFormat.format(calendar.getTime()) + "-robocode.br"));
        oos = new ObjectOutputStream(zos);
      } else if (format == BattleRecordFormat.XML) {
        final Charset utf8 = Charset.forName("UTF-8");

        osw = new OutputStreamWriter(bos, utf8);
        xwr = new XmlWriter(osw, true);
      } else if (format == BattleRecordFormat.XML_ZIP) {
        final Charset utf8 = Charset.forName("UTF-8");

        zos = new ZipOutputStream(bos);
        zos.putNextEntry(new ZipEntry(dateFormat.format(calendar.getTime()) + "-robocode.xml"));

        osw = new OutputStreamWriter(zos, utf8);
        xwr = new XmlWriter(osw, false);
      }

      if (isbin) {
        oos.writeObject(recordInfo);
      } else if (isxml) {
        xwr.startDocument();
        xwr.startElement("record");
        xwr.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        if (options.shortAttributes) {
          xwr.writeAttribute("xsi:noNamespaceSchemaLocation", "battleRecordS.xsd");
        } else {
          xwr.writeAttribute("xsi:noNamespaceSchemaLocation", "battleRecord.xsd");
        }
        recordInfo.writeXml(xwr, options);
        xwr.startElement("turns");
      }

      if (recordInfo.turnsInRounds != null) {
        fis = new FileInputStream(tempFile);
        bis = new BufferedInputStream(fis, 1024 * 1024);
        ois = new ObjectInputStream(bis);

        for (int i = 0; i < recordInfo.turnsInRounds.length; i++) {
          if (recordInfo.turnsInRounds[i] > 0) {
            for (int j = 0; j <= recordInfo.turnsInRounds[i] - 1; j++) {
              try {
                TurnSnapshot turn = (TurnSnapshot) ois.readObject();

                if (j != turn.getTurn()) {
                  throw new Error("Something rotten");
                }

                if (isbin) {
                  turn.stripDetails(options);
                  oos.writeObject(turn);
                } else if (isxml) {
                  turn.writeXml(xwr, options);
                }
              } catch (ClassNotFoundException e) {
                logError(e);
              }
            }
            if (isbin) {
              oos.flush();
            } else if (isxml) {
              osw.flush();
            }
            bos.flush();
            fos.flush();
          }
        }
        if (isxml) {
          xwr.endElement(); // turns
          xwr.endElement(); // record
          osw.flush();
        }
      }

    } catch (IOException e) {
      logError(e);
      recorder = new BattleRecorder(this, properties);
      createTempFile();
    } finally {
      FileUtil.cleanupStream(ois);
      FileUtil.cleanupStream(bis);
      FileUtil.cleanupStream(fis);
      FileUtil.cleanupStream(oos);
      FileUtil.cleanupStream(zos);
      FileUtil.cleanupStream(bos);
      FileUtil.cleanupStream(fos);
      FileUtil.cleanupStream(osw);
    }
  }
예제 #4
0
  public void loadRecord(String recordFilename, BattleRecordFormat format) {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    ZipInputStream zis = null;
    ObjectInputStream ois = null;
    InputStream xis = null;

    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ObjectOutputStream oos = null;

    try {
      createTempFile();
      fis = new FileInputStream(recordFilename);
      bis = new BufferedInputStream(fis, 1024 * 1024);

      if (format == BattleRecordFormat.BINARY) {
        ois = new ObjectInputStream(bis);
      } else if (format == BattleRecordFormat.BINARY_ZIP) {
        zis = new ZipInputStream(bis);
        zis.getNextEntry();
        ois = new ObjectInputStream(zis);
      } else if (format == BattleRecordFormat.XML_ZIP) {
        zis = new ZipInputStream(bis);
        zis.getNextEntry();
        xis = zis;
      } else if (format == BattleRecordFormat.XML) {
        xis = bis;
      }
      if (format == BattleRecordFormat.BINARY || format == BattleRecordFormat.BINARY_ZIP) {
        recordInfo = (BattleRecordInfo) ois.readObject();
        if (recordInfo.turnsInRounds != null) {
          fos = new FileOutputStream(tempFile);
          bos = new BufferedOutputStream(fos, 1024 * 1024);
          oos = new ObjectOutputStream(bos);

          for (int i = 0; i < recordInfo.turnsInRounds.length; i++) {
            for (int j = recordInfo.turnsInRounds[i] - 1; j >= 0; j--) {
              try {
                ITurnSnapshot turn = (ITurnSnapshot) ois.readObject();

                oos.writeObject(turn);
              } catch (ClassNotFoundException e) {
                logError(e);
              }
            }
          }
        }
      } else {
        final RecordRoot root = new RecordRoot();

        fos = new FileOutputStream(tempFile);
        bos = new BufferedOutputStream(fos, 1024 * 1024);
        root.oos = new ObjectOutputStream(bos);
        XmlReader.deserialize(xis, root);
        if (root.lastException != null) {
          logError(root.lastException);
        }
        recordInfo = root.recordInfo;
      }
    } catch (IOException e) {
      logError(e);
      createTempFile();
      recordInfo = null;
    } catch (ClassNotFoundException e) {
      if (e.getMessage().contains("robocode.recording.BattleRecordInfo")) {
        Logger.logError(
            "Sorry, backward compatibility with record from version 1.6 is not provided.");
      } else {
        logError(e);
      }
      createTempFile();
      recordInfo = null;
    } finally {
      FileUtil.cleanupStream(oos);
      FileUtil.cleanupStream(bos);
      FileUtil.cleanupStream(fos);
      FileUtil.cleanupStream(ois);
      FileUtil.cleanupStream(zis);
      FileUtil.cleanupStream(bis);
      FileUtil.cleanupStream(fis);
    }
  }