private List<List<Entry>> getSplits(List<Entry> all) { List<List<Entry>> tasks = new ArrayList<>(); long currentTaskSize = 0; List<Entry> currentTask = new ArrayList<>(); for (Entry entry : all) { currentTask.add(entry); currentTaskSize += entry.getSize(); // TODO consider to multiply the size by cost_per_byte, and add cost_per_file if (currentTaskSize >= minTaskSize) { tasks.add(currentTask); currentTask = new ArrayList<>(); currentTaskSize = 0; } } if (!currentTask.isEmpty()) { tasks.add(currentTask); } return tasks; } }
public synchronized boolean add(String path, long size) { // TODO throw IllegalStateException if stream is already closed if (!needsMore()) { return false; } if (!pathMatchPattern.matcher(path).find()) { return false; } int index = entries.size(); entries.add(new Entry(index, size)); byte[] data = path.getBytes(StandardCharsets.UTF_8); castBuffer.putInt(0, data.length); try { stream.write(castBuffer.array()); stream.write(data); } catch (IOException ex) { throw Throwables.propagate(ex); } last = path; return true; }
@Override public synchronized String get(int i) { Entry e = entries.get(i); if (e.getIndex() < current) { // rewind to the head try { stream.close(); stream = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))); } catch (IOException ex) { throw Throwables.propagate(ex); } current = 0; } while (current < e.getIndex()) { readNext(); } // now current == e.getIndex() return readNextString(); }