public void writeTasks(OutputStream output) { try { output.write("[\n".getBytes()); boolean needComma = false; File[] files = tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()); List<Integer> numbers = new ArrayList<>(); for (File directory : files) { try { numbers.add(Integer.valueOf(directory.getName())); } catch (Throwable ignored) { } } numbers.sort(Comparator.<Integer>reverseOrder()); numbers = numbers.subList(0, Math.min(100, numbers.size())); for (int taskId : numbers) { File infoFile = new File(new File(tasksDirectory, String.valueOf(taskId)), "info.json"); if (!infoFile.exists()) { continue; } if (needComma) { output.write(",\n".getBytes()); } else { needComma = true; } try (FileInputStream fis = new FileInputStream(infoFile)) { IOUtils.copy(fis, output); } } output.write("]\n".getBytes()); } catch (IOException e) { throw Throwables.propagate(e); } }
public void save(TaskInfo<?> taskInfo) { try { File taskDirectory = getTaskDirectory(taskInfo.getId()); FileUtils.forceMkdir(taskDirectory); File tmpFile = new File(taskDirectory, UUID.randomUUID().toString()); File infoFile = getTaskInfoFile(taskInfo.getId()); objectMapper.writeValue(tmpFile, taskInfo); Files.move( tmpFile.toPath(), infoFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw Throwables.propagate(e); } }
public TaskPersistence() { try { tasksDirectory = new File(System.getProperty("user.home") + "/.floto/tasks"); FileUtils.forceMkdir(tasksDirectory); long numberOfTasks = tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()).length; nextTaskId = new AtomicLong(numberOfTasks + 1); objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.registerModule(new JSR310Module()); } catch (IOException e) { throw Throwables.propagate(e); } }