コード例 #1
0
  protected void addItem(Crystal crystal) {
    FileInputStream stream;
    InputStreamReader streamReader;
    BufferedReader in;

    try {
      FileWriter w = new FileWriter(storageFile.toString());
      BufferedWriter out = new BufferedWriter(w);

      stream = new FileInputStream(storageFile);
      streamReader = new InputStreamReader(stream);
      in = new BufferedReader(streamReader);

      String line;
      boolean placed = false;
      while ((line = in.readLine()) != null) {
        if (crystal.getId() < Integer.valueOf(line.split("==")[0]) && !placed) {
          String crystalString =
              crystal.getId()
                  + "=="
                  + crystal.getExpirationDate()
                  + "=="
                  + Converters.toBase64(crystal.getContents());
          out.write(crystalString);
          out.newLine();
          placed = true;
        } else {
          out.write(line);
          out.newLine();
        }
      }

      if (!placed) {
        String crystalString =
            crystal.getId()
                + "=="
                + crystal.getExpirationDate()
                + "=="
                + Converters.toBase64(crystal.getContents());
        out.write(crystalString);
        out.newLine();
      }

      in.close();

      out.close();
      w.close();

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
コード例 #2
0
  public Crystal loadCrystal(int id) throws ExpiredCrystalException, CrystalNotFoundException {
    FileInputStream stream;
    InputStreamReader streamReader;
    BufferedReader in;

    try {
      stream = new FileInputStream(storageFile);
      streamReader = new InputStreamReader(stream);
      in = new BufferedReader(streamReader);

      String line;
      while ((line = in.readLine()) != null) {
        int itemID = Integer.valueOf(line.split("==")[0]);
        if (itemID == id) {
          in.close();
          if (Long.valueOf(line.split("==")[1]) < System.currentTimeMillis()) {
            removeItem(
                new Crystal(
                    this,
                    itemID,
                    Long.valueOf(line.split("==")[1]),
                    Converters.fromBase64(line.split("==")[2])));
            throw new ExpiredCrystalException(id, Long.valueOf(line.split("==")[1]));
          }
          return new Crystal(
              this,
              itemID,
              Long.valueOf(line.split("==")[1]),
              Converters.fromBase64(line.split("==")[2]));
        }
      }

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    throw new CrystalNotFoundException(id);
  }