/** * hessian protocol * * @param message message * @return chat message * @throws Exception exception */ public ChatMessage hessian(ChatMessage message) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Hessian2Output output = new Hessian2Output(bos); output.writeObject(message); output.flush(); byte[] content = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(content); Hessian2Input input = new Hessian2Input(bis); ChatMessage msg = (ChatMessage) input.readObject(ChatMessage.class); msg.setContentLength(content.length); return msg; }
@Override public AggregationInfo chronology(String dir, String targetFile) throws IOException { log.info("Try to aggregate {} into file {}", dir, targetFile); Collection<Hessian2Input> inputStreams = new ArrayList<Hessian2Input>(); Set<String> fileNameList = fileStorage.getFileNameList(dir); if (fileNameList.isEmpty()) { log.info("Nothing to aggregate. Directory {} is empty.", dir); new Hessian2Output(fileStorage.create(targetFile)).close(); return new AggregationInfo(0, 0, 0); } for (String fileName : fileNameList) { try { InputStream in = fileStorage.open(fileName); inputStreams.add(new Hessian2Input(in)); } catch (FileNotFoundException e) { log.warn(e.getMessage(), e); } } int count = 0; long minTime = 0; long maxTime = 0; Hessian2Output out = null; OutputStream os = null; try { if (fileStorage.delete(targetFile, false)) { log.warn("Target file {} did not deleted!", targetFile); } os = fileStorage.create(targetFile); out = new Hessian2Output(os); MinMaxPriorityQueue<StreamInfo> queue = MinMaxPriorityQueue.create(); for (Hessian2Input inputStream : inputStreams) { LogEntry logEntry; try { logEntry = (LogEntry) inputStream.readObject(); } catch (EOFException e) { continue; } queue.add(new StreamInfo(inputStream, logEntry)); } while (!queue.isEmpty()) { StreamInfo<LogEntry> streamInfo = queue.removeFirst(); out.writeObject(streamInfo.lastLogEntry); if (count == 0) { minTime = streamInfo.lastLogEntry.getTime(); maxTime = streamInfo.lastLogEntry.getTime(); } else { maxTime = streamInfo.lastLogEntry.getTime(); } count++; LogEntry logEntry; try { logEntry = (LogEntry) streamInfo.stream.readObject(); } catch (EOFException e) { continue; } streamInfo.lastLogEntry = logEntry; queue.add(streamInfo); } } finally { if (out != null) { out.close(); os.close(); } } return new AggregationInfo(minTime, maxTime, count); }