protected String getJson(String url) throws IOException { Request okRequest = new Request.Builder().url(url).build(); ResponseBody body = null; try { body = downloader.newCall(okRequest).execute().body(); return body.string(); } finally { Helper.close(body); } }
protected String postJson(String url, JsonNode data) throws IOException { Request okRequest = new Request.Builder().url(url).post(RequestBody.create(MT_JSON, data.toString())).build(); ResponseBody body = null; try { body = downloader.newCall(okRequest).execute().body(); return body.string(); } finally { Helper.close(body); } }
@Override byte[] readFile(File file) throws IOException { InputStream is = new FileInputStream(file); BufferedInputStream buff = new BufferedInputStream(is); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); close(buff); return os.toByteArray(); }
@Override byte[] readFile(File file) throws IOException { InputStream is = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(is); zis.getNextEntry(); BufferedInputStream buff = new BufferedInputStream(zis); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); Helper.close(buff); return os.toByteArray(); }
public void downloadFile(String url, String toFile) throws IOException { HttpURLConnection conn = createConnection(url); InputStream iStream = fetch(conn, false); int size = 8 * 1024; BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), size); InputStream in = new BufferedInputStream(iStream, size); try { byte[] buffer = new byte[size]; int numRead; while ((numRead = in.read(buffer)) != -1) { writer.write(buffer, 0, numRead); } } finally { Helper.close(iStream); Helper.close(writer); Helper.close(in); } }
/** * @param forceClean if true the clean hack (system.gc) will be executed and forces the system * to cleanup the mmap resources. Set false if you need to close many MMapDataAccess objects. */ void close(boolean forceClean) { clean(0, segments.size()); segments.clear(); Helper.close(raFile); if (forceClean) Helper.cleanHack(); }
@Override Raster generateRasterFromFile(File file, String tifName) { SeekableStream ss = null; try { InputStream is = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(is); // find tif file in zip ZipEntry entry = zis.getNextEntry(); while (entry != null && !entry.getName().equals(tifName)) { entry = zis.getNextEntry(); } ss = SeekableStream.wrapInputStream(zis, true); TIFFImageDecoder imageDecoder = new TIFFImageDecoder(ss, new TIFFDecodeParam()); return imageDecoder.decodeAsRaster(); } catch (Exception e) { throw new RuntimeException("Can't decode " + tifName, e); } finally { if (ss != null) Helper.close(ss); } }
@Override Raster generateRasterFromFile(File file, String tifName) { SeekableStream ss = null; try { InputStream is = new FileInputStream(file); ss = SeekableStream.wrapInputStream(is, true); TIFFImageDecoder imageDecoder = new TIFFImageDecoder(ss, new TIFFDecodeParam()); return imageDecoder.decodeAsRaster(); } catch (Exception e) { throw new RuntimeException("Can't decode " + file.getName(), e); } finally { if (ss != null) close(ss); } }
@After public void tearDown() { Helper.close(graph); }
@After public void tearDown() { Helper.close((Closeable) graph); Helper.removeDir(new File(locationParent)); }
@After public void tearDown() { Helper.close((Closeable) graph); Helper.removeDir(new File(locationParent)); }
@Override public GHResponse route(GHRequest request) { ResponseBody rspBody = null; try { Request okRequest = createRequest(request); rspBody = getClientForRequest(request).newCall(okRequest).execute().body(); JsonNode json = objectMapper.reader().readTree(rspBody.byteStream()); GHResponse res = new GHResponse(); res.addErrors(readErrors(json)); if (res.hasErrors()) return res; JsonNode paths = json.get("paths"); boolean tmpElevation = request.getHints().getBool("elevation", elevation); boolean tmpTurnDescription = request.getHints().getBool("turn_description", true); for (JsonNode path : paths) { PathWrapper altRsp = createPathWrapper(path, tmpElevation, tmpTurnDescription); res.add(altRsp); } return res; } catch (Exception ex) { throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex); } finally { Helper.close(rspBody); } }
@Test public void testNoErrorOnEdgeCase_lastIndex() { final EncodingManager encodingManager = new EncodingManager("car"); int locs = 10000; Graph g = AbstractLocationIndexTester.this.createGHStorage(new MMapDirectory(location), encodingManager, false); NodeAccess na = g.getNodeAccess(); Random rand = new Random(12); for (int i = 0; i < locs; i++) { na.setNode(i, (float) rand.nextDouble() * 10 + 10, (float) rand.nextDouble() * 10 + 10); } idx = createIndex(g, 200); Helper.close((Closeable) g); }
@Test public void testGetLocations() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 12, 23); na.setNode(1, 22, 23); assertEquals(2, graph.getNodes()); graph.edge(0, 1, 10, true); assertEquals(2, graph.getNodes()); graph.edge(0, 2, 10, true); assertEquals(3, graph.getNodes()); Helper.close((Closeable) graph); graph = createGHStorage(); assertEquals(0, graph.getNodes()); }
@Test public void testSinglePoints120() { Graph g = createSampleGraph(new EncodingManager("car")); idx = createIndex(g, -1); assertEquals(1, findID(idx, 1.637, 2.23)); assertEquals(10, findID(idx, 3.649, 1.375)); assertEquals(9, findID(idx, 3.3, 2.2)); assertEquals(6, findID(idx, 3.0, 1.5)); assertEquals(10, findID(idx, 3.8, 0)); assertEquals(10, findID(idx, 3.8466, 0.021)); Helper.close((Closeable) g); }
@Test public void testSinglePoints32() { Graph g = createSampleGraph(new EncodingManager("car")); idx = createIndex(g, -1); // 10 or 6 assertEquals(10, findID(idx, 3.649, 1.375)); assertEquals(10, findID(idx, 3.8465748, 0.021762699)); if (hasEdgeSupport()) { assertEquals(4, findID(idx, 2.485, 1.373)); } else { assertEquals(6, findID(idx, 2.485, 1.373)); } assertEquals(0, findID(idx, 0.64628404, 0.53006625)); Helper.close((Closeable) g); }
@Test public void testSimpleGraph() { Graph g = AbstractLocationIndexTester.this.createGHStorage(new EncodingManager("car")); initSimpleGraph(g); idx = createIndex(g, -1); assertEquals(4, findID(idx, 5, 2)); assertEquals(3, findID(idx, 1.5, 2)); assertEquals(0, findID(idx, -1, -1)); if (hasEdgeSupport()) // now get the edge 1-4 and not node 6 { assertEquals(4, findID(idx, 4, 0)); } else { assertEquals(6, findID(idx, 4, 0)); } Helper.close((Closeable) g); }
@Test public void testDifferentVehicles() { final EncodingManager encodingManager = new EncodingManager("car,foot"); Graph g = AbstractLocationIndexTester.this.createGHStorage(encodingManager); initSimpleGraph(g); idx = createIndex(g, -1); assertEquals(1, findID(idx, 1, -1)); // now make all edges from node 1 accessible for CAR only EdgeIterator iter = g.createEdgeExplorer().setBaseNode(1); CarFlagEncoder carEncoder = (CarFlagEncoder) encodingManager.getEncoder("car"); while (iter.next()) { iter.setFlags(carEncoder.setProperties(50, true, true)); } idx.close(); idx = createIndex(g, -1); FootFlagEncoder footEncoder = (FootFlagEncoder) encodingManager.getEncoder("foot"); assertEquals(2, idx.findClosest(1, -1, DefaultEdgeFilter.allEdges(footEncoder)).getClosestNode()); Helper.close((Closeable) g); } }
@Test public void testClone() { graph = createGHStorage(); graph.edge(1, 2, 10, true); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 12, 23); na.setNode(1, 8, 13); na.setNode(2, 2, 10); na.setNode(3, 5, 9); graph.edge(1, 3, 10, true); Graph cloneGraph = graph.copyTo(AbstractGraphStorageTester.this.createGHStorage(locationParent + "/clone", false)); assertEquals(graph.getNodes(), cloneGraph.getNodes()); assertEquals(count(carOutExplorer.setBaseNode(1)), count(cloneGraph.createEdgeExplorer(carOutFilter).setBaseNode(1))); cloneGraph.edge(1, 4, 10, true); assertEquals(3, count(cloneGraph.createEdgeExplorer(carOutFilter).setBaseNode(1))); assertEquals(graph.getBounds(), cloneGraph.getBounds()); Helper.close((Closeable) cloneGraph); }
@Test public void testSimpleGraph2() { Graph g = AbstractLocationIndexTester.this.createGHStorage(new EncodingManager("car")); initSimpleGraph(g); idx = createIndex(g, -1); assertEquals(4, findID(idx, 5, 2)); assertEquals(3, findID(idx, 1.5, 2)); assertEquals(0, findID(idx, -1, -1)); assertEquals(6, findID(idx, 4.5, -0.5)); if (hasEdgeSupport()) { assertEquals(4, findID(idx, 4, 1)); assertEquals(4, findID(idx, 4, 0)); } else { assertEquals(6, findID(idx, 4, 1)); assertEquals(6, findID(idx, 4, 0)); } assertEquals(6, findID(idx, 4, -2)); assertEquals(5, findID(idx, 3, 3)); Helper.close((Closeable) g); }