FileBackedSpatialBinCollector implements SpatialBinCollector { static void readFromStream(DataInputStream dis, SortedMap<Long, List<SpatialBin>> map) throws IOException { while (true) { try { long binIndex = dis.readLong(); List<SpatialBin> spatialBins = map.get(binIndex); if (spatialBins == null) { spatialBins = new ArrayList<>(); map.put(binIndex, spatialBins); } spatialBins.add(SpatialBin.read(binIndex, dis)); } catch (EOFException eof) { return; } } } FileBackedSpatialBinCollector(long maximumNumberOfBins); @Override void consumeSpatialBins(BinningContext ignored, List<SpatialBin> spatialBins); @Override void consumingCompleted(); @Override SpatialBinCollection getSpatialBinCollection(); void close();  }
@Test public void testReadFromStream() throws Exception { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(byteStream); dos.writeLong(23); SpatialBin bin1 = createSpatialBin(23); bin1.write(dos); dos.writeLong(23); SpatialBin bin2 = createSpatialBin(23); bin2.write(dos); dos.writeLong(1048); SpatialBin bin3 = createSpatialBin(1048); bin3.write(dos); dos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(byteStream.toByteArray()); TreeMap<Long, List<SpatialBin>> map = new TreeMap<>(); FileBackedSpatialBinCollector.readFromStream(new DataInputStream(bais), map); assertEquals(2, map.size()); assertTrue(map.containsKey(23L)); assertTrue(map.containsKey(1048L)); List<SpatialBin> spatialBins23 = map.get(23L); assertEquals(2, spatialBins23.size()); assertEquals(bin2.getFeatureValues()[0], spatialBins23.get(1).getFeatureValues()[0], 1.0e-6); }