List of usage examples for java.nio.channels Channels newChannel
public static WritableByteChannel newChannel(OutputStream out)
From source file:com.alibaba.jstorm.daemon.nimbus.ServiceHandler.java
/** * prepare to uploading topology jar, return the file location *///from ww w .j a v a 2 s . c o m @Override public String beginFileUpload() throws TException { String fileLoc = null; try { String path; String key = UUID.randomUUID().toString(); path = StormConfig.masterInbox(conf) + "/" + key; FileUtils.forceMkdir(new File(path)); FileUtils.cleanDirectory(new File(path)); fileLoc = path + "/stormjar-" + key + ".jar"; data.getUploaders().put(fileLoc, Channels.newChannel(new FileOutputStream(fileLoc))); LOG.info("Begin upload file from client to " + fileLoc); return path; } catch (FileNotFoundException e) { LOG.error("File not found: " + fileLoc, e); throw new TException(e); } catch (IOException e) { LOG.error("Upload file error: " + fileLoc, e); throw new TException(e); } }
From source file:org.cytoscape.app.internal.net.WebQuerier.java
/** * Given the unique app name used by the app store, query the app store for the * download URL and download the app to the given directory. * /*from w w w . ja v a 2s . c o m*/ * If a file with the same name exists in the directory, it is overwritten. * * @param appName The unique app name used by the app store * @param version The desired version, or <code>null</code> to obtain the latest release * @param directory The directory used to store the downloaded file * @param taskMonitor */ public File downloadApp(WebApp webApp, String version, File directory, DownloadStatus status) throws AppDownloadException { List<WebApp.Release> compatibleReleases = getCompatibleReleases(webApp); if (compatibleReleases.size() > 0) { WebApp.Release releaseToDownload = null; if (version != null) { for (WebApp.Release compatibleRelease : compatibleReleases) { // Check if the desired version is found in the list of available versions if (compatibleRelease.getReleaseVersion() .matches("(^\\s*|.*,)\\s*" + version + "\\s*(\\s*$|,.*)")) { releaseToDownload = compatibleRelease; } } if (releaseToDownload == null) { throw new AppDownloadException("No release with the requested version " + version + " was found for the requested app " + webApp.getFullName()); } } else { releaseToDownload = compatibleReleases.get(compatibleReleases.size() - 1); } URL downloadUrl = null; try { downloadUrl = new URL(currentAppStoreUrl + releaseToDownload.getRelativeUrl()); } catch (MalformedURLException e) { throw new AppDownloadException("Unable to obtain URL for version " + version + " of the release for " + webApp.getFullName()); } if (downloadUrl != null) { try { // Prepare to download URLConnection connection = streamUtil.getURLConnection(downloadUrl); InputStream inputStream = connection.getInputStream(); long contentLength = connection.getContentLength(); ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream); File outputFile; try { // Replace spaces with underscores String outputFileBasename = webApp.getName().replaceAll("\\s", "_"); // Append version information outputFileBasename += "-v" + releaseToDownload.getReleaseVersion(); // Strip disallowed characters outputFileBasename = OUTPUT_FILENAME_DISALLOWED_CHARACTERS.matcher(outputFileBasename) .replaceAll(""); // Append extension outputFileBasename += ".jar"; // Output file has same name as app, but spaces and slashes are replaced with hyphens outputFile = new File(directory.getCanonicalPath() + File.separator + outputFileBasename); if (outputFile.exists()) { outputFile.delete(); } outputFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); try { FileChannel fileChannel = fileOutputStream.getChannel(); long currentDownloadPosition = 0; long bytesTransferred; TaskMonitor taskMonitor = status.getTaskMonitor(); do { bytesTransferred = fileChannel.transferFrom(readableByteChannel, currentDownloadPosition, 1 << 14); if (status.isCanceled()) { outputFile.delete(); return null; } currentDownloadPosition += bytesTransferred; if (contentLength > 0) { double progress = (double) currentDownloadPosition / contentLength; taskMonitor.setProgress(progress); } } while (bytesTransferred > 0); } finally { fileOutputStream.close(); } } finally { readableByteChannel.close(); } return outputFile; } catch (IOException e) { throw new AppDownloadException( "Error while downloading app " + webApp.getFullName() + ", " + e.getMessage()); } } } else { throw new AppDownloadException( "No available releases were found for the app " + webApp.getFullName() + "."); } return null; }
From source file:com.linkedin.databus.core.TestDbusEventBufferMult.java
@Test public void testMultiPPartionStreamStats() throws Exception { createBufMult();/*from w w w . ja v a 2s . c o m*/ PhysicalPartition[] p = { _pConfigs[0].getPhysicalPartition(), _pConfigs[1].getPhysicalPartition(), _pConfigs[2].getPhysicalPartition() }; //generate a bunch of windows for 3 partitions int windowsNum = 10; for (int i = 1; i <= windowsNum; ++i) { DbusEventBufferAppendable buf = _eventBufferMult.getDbusEventBufferAppendable(p[0]); buf.startEvents(); byte[] schema = "abcdefghijklmnop".getBytes(Charset.defaultCharset()); assertTrue(buf.appendEvent(new DbusEventKey(1), (short) 100, (short) 0, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[10], false, null)); buf.endEvents(100 * i, null); buf = _eventBufferMult.getDbusEventBufferAppendable(p[1]); buf.startEvents(); assertTrue(buf.appendEvent(new DbusEventKey(1), (short) 101, (short) 2, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[100], false, null)); assertTrue(buf.appendEvent(new DbusEventKey(2), (short) 101, (short) 2, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[10], false, null)); buf.endEvents(100 * i + 1, null); buf = _eventBufferMult.getDbusEventBufferAppendable(p[2]); buf.startEvents(); assertTrue(buf.appendEvent(new DbusEventKey(1), (short) 101, (short) 2, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[100], false, null)); assertTrue(buf.appendEvent(new DbusEventKey(2), (short) 101, (short) 2, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[10], false, null)); assertTrue(buf.appendEvent(new DbusEventKey(3), (short) 101, (short) 2, System.currentTimeMillis() * 1000000, (short) 2, schema, new byte[10], false, null)); buf.endEvents(100 * i + 2, null); } String[] pnames = new String[p.length]; int count = 0; for (PhysicalPartition ip : p) { pnames[count++] = ip.toSimpleString(); } StatsCollectors<DbusEventsStatisticsCollector> statsColl = createStats(pnames); PhysicalPartitionKey[] pkeys = { new PhysicalPartitionKey(p[0]), new PhysicalPartitionKey(p[1]), new PhysicalPartitionKey(p[2]) }; CheckpointMult cpMult = new CheckpointMult(); for (int i = 0; i < 3; ++i) { Checkpoint cp = new Checkpoint(); cp.setFlexible(); cp.setConsumptionMode(DbusClientMode.ONLINE_CONSUMPTION); cpMult.addCheckpoint(p[i], cp); } DbusEventBufferBatchReadable reader = _eventBufferMult.getDbusEventBufferBatchReadable(cpMult, Arrays.asList(pkeys), statsColl); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel writeChannel = Channels.newChannel(baos); reader.streamEvents(false, 1000000, writeChannel, Encoding.BINARY, new AllowAllDbusFilter()); writeChannel.close(); baos.close(); //make sure we got the physical partition names right List<String> ppartNames = statsColl.getStatsCollectorKeys(); assertEquals(ppartNames.size(), 3); HashSet<String> expectedPPartNames = new HashSet<String>( Arrays.asList(p[0].toSimpleString(), p[1].toSimpleString(), p[2].toSimpleString())); for (String ppartName : ppartNames) { assertTrue(expectedPPartNames.contains(ppartName)); } //verify event counts per partition DbusEventsTotalStats[] ppartStats = { statsColl.getStatsCollector(p[0].toSimpleString()).getTotalStats(), statsColl.getStatsCollector(p[1].toSimpleString()).getTotalStats(), statsColl.getStatsCollector(p[2].toSimpleString()).getTotalStats() }; assertEquals(ppartStats[0].getNumDataEvents(), windowsNum); assertEquals(ppartStats[1].getNumDataEvents(), windowsNum * 2); assertEquals(ppartStats[2].getNumDataEvents(), windowsNum * 3); assertEquals(ppartStats[0].getNumSysEvents(), windowsNum); assertEquals(ppartStats[1].getNumSysEvents(), windowsNum); assertEquals(ppartStats[2].getNumSysEvents(), windowsNum); assertEquals(statsColl.getStatsCollector().getTotalStats().getNumDataEvents(), windowsNum * (1 + 2 + 3)); assertEquals(statsColl.getStatsCollector().getTotalStats().getNumSysEvents(), windowsNum * 3); assertEquals(statsColl.getStatsCollector().getTotalStats().getMaxTimeLag(), Math .max(ppartStats[0].getTimeLag(), Math.max(ppartStats[1].getTimeLag(), ppartStats[2].getTimeLag()))); assertEquals(statsColl.getStatsCollector().getTotalStats().getMinTimeLag(), Math .min(ppartStats[0].getTimeLag(), Math.min(ppartStats[1].getTimeLag(), ppartStats[2].getTimeLag()))); }
From source file:org.alfresco.contentstore.ChecksumTest.java
@Test public void test3() throws IOException { checksumService.setBlockSize(8192);/* w w w . j a va2s . c om*/ UserContext.setUser("user1"); File f = TempFileProvider.createTempFile("ContentStoreTest", GUID.generate()); // File f = copy("marbles-uncompressed.tif"); System.out.println("f = " + f); Node node = Node.build().nodeId(GUID.generate()).nodeVersion(1l); ClassLoader cl = getClass().getClassLoader(); try (InputStream in = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed.tif")) { NodeChecksums checksums = checksumService.getChecksums(node, in); try (ReadableByteChannel destChannel = Channels .newChannel(cl.getResourceAsStream("marbles-uncompressed1.tif")); ReadableByteChannel srcChannel = Channels .newChannel(cl.getResourceAsStream("marbles-uncompressed.tif")); FileOutputStream fos = new FileOutputStream(f); WritableByteChannel patchedChannel = fos.getChannel()) { PatchDocument patchDocument = new PatchDocumentImpl(); patchService.updatePatchDocument(patchDocument, checksums, destChannel); System.out.println("patchDocument = " + patchDocument); int totalWritten = applyPatch(srcChannel, patchedChannel, patchDocument); System.out.println("totalWritten = " + totalWritten); } } try (InputStream in3 = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed1.tif"); InputStream in4 = new FileInputStream(f)) { assertEqual(in3, in4); } }
From source file:com.pavlospt.rxfile.RxFile.java
private static File fileFromUri(Context context, Uri data) throws Exception { DocumentFile file = DocumentFile.fromSingleUri(context, data); String fileType = file.getType(); String fileName = file.getName(); File fileCreated;/* w ww. java2 s . com*/ ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data, Constants.READ_MODE); InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor()); logDebug("External cache dir:" + context.getExternalCacheDir()); String filePath = context.getExternalCacheDir() + Constants.FOLDER_SEPARATOR + fileName; String fileExtension = fileName.substring((fileName.lastIndexOf('.')) + 1); String mimeType = getMimeType(fileName); logDebug("From Google Drive guessed type: " + getMimeType(fileName)); logDebug("Extension: " + fileExtension); if (fileType.equals(Constants.APPLICATION_PDF) && mimeType == null) { filePath += "." + Constants.PDF_EXTENSION; } if (!createFile(filePath)) { return new File(filePath); } ReadableByteChannel from = Channels.newChannel(inputStream); WritableByteChannel to = Channels.newChannel(new FileOutputStream(filePath)); fastChannelCopy(from, to); from.close(); to.close(); fileCreated = new File(filePath); logDebug("Path for made file: " + fileCreated.getAbsolutePath()); return fileCreated; }
From source file:org.alfresco.contentstore.ContentStoreTest.java
@Test public void test5() throws IOException { checksumService.setBlockSize(8192);//w w w. j a va2 s . c o m UserContext.setUser("user1"); String nodeId = GUID.generate(); long nodeVersion = 1l; MimeType mimeType = MimeType.XLSX; Node node = Node.build().nodeId(nodeId).nodeVersion(nodeVersion).mimeType(mimeType); File f = copy("marbles-uncompressed.tif"); try (InputStream in = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed.tif")) { NodeChecksums checksums = checksumService.getChecksums(node, in); System.out.println("checksums = " + checksums); try (InputStream in1 = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed1.tif"); ReadableByteChannel channel1 = Channels.newChannel(in1)) { PatchDocument patchDocument = new PatchDocumentImpl(); patchService.updatePatchDocument(patchDocument, checksums, channel1); System.out.println("patchDocument = " + patchDocument); applyPatch(f, patchDocument); } } State state = new State(); try (InputStream in1 = new FileInputStream(f); InputStream in2 = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed1.tif")) { assertFileEquals(in1, in2, state); } }
From source file:Interface.FramePrincipal.java
private void bt_dowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_dowActionPerformed File dir = new File(dir_dow.getText()); if ("".equals(dir_dow.getText())) { JOptionPane.showMessageDialog(null, "Campo diretrio no pode estar vazio!"); } else if (!dir.exists()) { JOptionPane.showMessageDialog(null, "Informe um diretrio vlido!"); } else {/* ww w . j a v a 2s . c om*/ try { URL arquivoBucket = new URL("https://console.developers.google.com/m/cloudstorage/b/" + bac.getNome() + "/o/" + arquivo); //Passa caminho de saida do arquivo que esta sendo baixado ReadableByteChannel canalArquivoSaida = Channels.newChannel(arquivoBucket.openStream()); FileOutputStream arquivoSaida = new FileOutputStream(dir_dow.getText() + "/" + arquivo); //Calcula tempo que o processo de download levou long inicio = System.currentTimeMillis(); arquivoSaida.getChannel().transferFrom(canalArquivoSaida, 0, 1 << 24); long fim = System.currentTimeMillis(); System.out.println(fim - inicio); JOptionPane.showMessageDialog(null, "Arquivo baixado com sucesso!"); arquivoSaida.close(); //libera o arquivo aps ser baixado. } catch (Exception e) { JOptionPane.showMessageDialog(null, "Arquivo no possui permisso para download!"); } } }
From source file:org.geowebcache.GeoWebCacheDispatcher.java
private void writeFixedResponse(HttpServletResponse response, int httpCode, String contentType, Resource resource, CacheResult cacheRes, int contentLength) { response.setStatus(httpCode);/* w w w . j a va 2 s . c o m*/ response.setContentType(contentType); response.setContentLength((int) contentLength); if (resource != null) { try { OutputStream os = response.getOutputStream(); resource.transferTo(Channels.newChannel(os)); runtimeStats.log(contentLength, cacheRes); } catch (IOException ioe) { log.debug("Caught IOException: " + ioe.getMessage() + "\n\n" + ioe.toString()); } } }
From source file:org.wymiwyg.wrhapi.test.BaseTests.java
/** * test is the returned status code matches the one of the HandlerException * thrown, with a HandlerException thrown after a body is set * /*from www . ja v a 2 s. c o m*/ * @throws Exception * on failure */ /*public void testExceptionStatusCodeAfterBody() throws Exception { final int statusCode = 302; WebServer webServer = createServer().startNewWebServer(new Handler() { public void handle(Request request, Response response) throws HandlerException { log.info("handling testStatusCode"); response.setHeader(HeaderName.SERVER, "Ad-Hoc testing server"); response.setBody(new MessageBody2Write() { public void writeTo(WritableByteChannel out) throws IOException { out.write(ByteBuffer.wrap("my body\n\ncontent\n" .getBytes())); } }); throw new HandlerException(ResponseStatus.getInstanceByCode(statusCode)); } }, serverBinding); try { URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":" + serverBinding.getPort() + "/"); HttpClient client = new HttpClient(); HttpMethod method = new HeadMethod(serverURL.toString()); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); client.executeMethod(method); // for the handler to be invoked, something of the response has to // be asked assertEquals(statusCode, method.getStatusCode()); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { webServer.stop(); } }*/ @Test public void testRepeated() throws Exception { final String body = "This is the content of the body"; final boolean writeBody = false; WebServer webServer = createServer().startNewWebServer(new Handler() { public void handle(Request request, Response response) throws HandlerException { log.info("handling testSimpleBody"); if (writeBody) { response.setBody(new MessageBody2Write() { public void writeTo(WritableByteChannel out) throws IOException { out.write(ByteBuffer.wrap(body.getBytes())); } }); } else { response.setBody(new MessageBody2Read() { public ReadableByteChannel read() throws IOException { return Channels.newChannel(new ByteArrayInputStream(body.getBytes())); } }); } } }, serverBinding); TimeLogger tl = new TimeLogger(); for (int r = 0; r < 10000; r++) { tl.startSection("iteration"); URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":" + serverBinding.getPort() + "/"); Reader reader = new InputStreamReader(serverURL.openStream()); StringWriter stringWriter = new StringWriter(); for (int ch = reader.read(); ch != -1; ch = reader.read()) { stringWriter.write(ch); } assertEquals(body, stringWriter.toString()); tl.endSection(); } webServer.stop(); tl.writeReport(new PrintWriter(System.out)); System.out.println("Using SummaryReportWriter:"); tl.setReportWriter(new SummaryReportWriter()); tl.writeReport(new PrintWriter(System.out)); }
From source file:org.alfresco.contentstore.ChecksumTest.java
@Test public void test11() throws IOException { try (InputStream in = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed.tif"); InputStream in1 = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed1.tif")) { ByteBuffer buf1 = ByteBuffer.allocate(8192); ByteBuffer buf2 = ByteBuffer.allocate(8192); ReadableByteChannel channel1 = Channels.newChannel(in); ReadableByteChannel channel2 = Channels.newChannel(in1); int numRead1 = -1; int numRead2 = -1; int total = 0; int same = 0; int i = 0; do {/* w ww .jav a2 s . co m*/ total = 0; same = 0; numRead1 = channel1.read(buf1); numRead2 = channel2.read(buf2); i += 8192; // buf1.clear(); // buf2.clear(); // // numRead1 = channel1.read(buf1); // numRead2 = channel2.read(buf2); buf1.flip(); buf2.flip(); if (numRead1 > 0 && numRead2 > 0) { if (numRead1 <= numRead2) { while (buf1.hasRemaining()) { total++; byte b1 = buf1.get(); byte b2 = buf2.get(); if (b1 == b2) { same++; } } } else { while (buf2.hasRemaining()) { total++; byte b1 = buf1.get(); byte b2 = buf2.get(); if (b1 == b2) { same++; } } } } buf1.clear(); buf2.clear(); } while (numRead1 > 0 && numRead2 > 0 && same < total); // while(numRead1 > 0 && numRead1 == numRead2); System.out.println(i + ", " + numRead1 + ", " + numRead2 + ", " + total + ", " + same + ", " + (double) same / total); } }