FileBackedSpatialBinCollector implements SpatialBinCollector { static void writeToStream(List<SpatialBin> spatialBins, DataOutputStream dos) throws IOException { for (SpatialBin spatialBin : spatialBins) { dos.writeLong(spatialBin.getIndex()); spatialBin.write(dos); } } FileBackedSpatialBinCollector(long maximumNumberOfBins); @Override void consumeSpatialBins(BinningContext ignored, List<SpatialBin> spatialBins); @Override void consumingCompleted(); @Override SpatialBinCollection getSpatialBinCollection(); void close();  }
@Test public void testWriteToStream() throws Exception { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(byteStream); ArrayList<SpatialBin> binList = new ArrayList<>(); binList.add(createSpatialBin(42)); binList.add(createSpatialBin(42)); binList.add(createSpatialBin(42)); binList.add(createSpatialBin(42)); FileBackedSpatialBinCollector.writeToStream(binList, dos); ByteArrayInputStream bais = new ByteArrayInputStream(byteStream.toByteArray()); TreeMap<Long, List<SpatialBin>> newMap = new TreeMap<>(); FileBackedSpatialBinCollector.readFromStream(new DataInputStream(bais), newMap); assertEquals(1, newMap.size()); List<SpatialBin> newBinList = newMap.get(42L); assertEquals(4, newBinList.size()); }