示例#1
0
 protected File setData(String data, String suffix) {
   // uuid timestamp script
   UUID gen = UUID.randomUUID();
   File script = new File(String.valueOf(gen.toString()) + suffix);
   // duplicate avoidance
   while (script.exists()) {
     // try to wait  a short random time
     try {
       wait((int) (Math.random() * 100));
     } catch (InterruptedException e) {
       LogHandler.writeStackTrace(log, e, Level.WARNING);
     }
     gen = UUID.randomUUID();
     script = new File(String.valueOf(gen.timestamp()));
   }
   // Writes data to script
   PrintWriter pw = null;
   try {
     script.createNewFile();
     pw = new PrintWriter(script);
   } catch (IOException e) {
     LogHandler.writeStackTrace(log, e, Level.SEVERE);
     return null;
   }
   pw.write(data);
   pw.close();
   return script;
 }
  /**
   * Solution taken from
   * http://stackoverflow.com/questions/13070674/get-the-unix-timestamp-from-type-1-uuid
   *
   * @param uuid type 1 (time based) uuid
   * @return unixTimestamp of uuid
   */
  public long toUnixTimestamp(final UUID uuid) {
    final Calendar uuidEpoch = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    uuidEpoch.clear();
    uuidEpoch.set(1582, OCTOBER, 15, 0, 0, 0);
    final long epochMillis = uuidEpoch.getTime().getTime();

    return (uuid.timestamp() / 10000L) + epochMillis;
  }