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); } }
@SuppressWarnings("deprecation") public void writeLogs(String taskId, OutputStream output) { try (FileInputStream input = new FileInputStream(getLogFile(taskId))) { DataInputStream dataInput = new DataInputStream(input); output.write("[\n".getBytes()); byte[] buffer = new byte[4 * 1024]; boolean needComma = false; try { while (true) { IOUtils.skip(input, 1); String line = dataInput.readLine(); if (line == null || line.isEmpty()) { break; } int length = Integer.parseInt(line); if (length > buffer.length) { buffer = new byte[length]; } IOUtils.readFully(input, buffer, 0, length); if (needComma) { output.write(",\n".getBytes()); } else { needComma = true; } output.write(buffer, 0, length); } } catch (EOFException ignored) { // EOF reached } output.write("]\n".getBytes()); } catch (Throwable throwable) { Throwables.propagate(throwable); } }
public void addLogEntry(String id, LogEntry logEntry) { try { OutputStream outputStream = logStreamCache.getUnchecked(id); ByteArrayOutputStream baos = new ByteArrayOutputStream(); objectMapper.writer().writeValue(baos, logEntry); synchronized (outputStream) { outputStream.write(("\n" + baos.size() + "\n").getBytes()); outputStream.write(baos.toByteArray()); } } catch (Throwable ignored) { // do not log errors when logging } }