List of usage examples for java.nio.channels Channels newChannel
public static WritableByteChannel newChannel(OutputStream out)
From source file:com.sunchenbin.store.feilong.core.io.IOWriteUtil.java
/** * NIO API ?? ()./* w ww .j a v a 2 s .co m*/ * * <h3>NIO</h3> * * <blockquote> * <p> * nionew io,jdk1.4,??<br> * nio??<br> * </p> * * <p> * ??,<br> * ?,,??? ????cpu,?cpu? <br> * ? Java IO ,? IO,NIO ?? IO * </p> * * <p> * The new I/O (NIO) APIs introduced in v 1.4 provide new features and improved performance in the areas of buffer management, scalable * network and file I/O, character-set support, and regular-expression matching. The NIO APIs supplement the I/O facilities in the * java.io package. * </p> * * <p> * The NIO APIs include the following features: * </p> * * <ol> * <li>Buffers for data of primitive types</li> * <li>Character-set encoders and decoders</li> * <li>A pattern-matching facility based on Perl-style regular expressions</li> * <li>Channels, a new primitive I/O abstraction</li> * <li>A file interface that supports locks and memory mapping</li> * <li>A multiplexed, non-blocking I/O facility for writing scalable servers</li> * </ol> * </blockquote> * * <p> * As creme de la creme with regard to performance,you could use NIO {@link java.nio.channels.Channels} and {@link java.nio.ByteBuffer}. * </p> * * @param bufferLength * the buffer length * @param inputStream * the input stream * @param outputStream * the output stream * @since 1.0.8 * @since jdk1.4 */ private static void writeUseNIO(int bufferLength, InputStream inputStream, OutputStream outputStream) { Date beginDate = new Date(); ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream); WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength); try { int loopCount = 0; int sumSize = 0; while (readableByteChannel.read(byteBuffer) != -1) { byteBuffer.flip(); sumSize += writableByteChannel.write(byteBuffer); byteBuffer.clear(); loopCount++; } if (LOGGER.isDebugEnabled()) { String formatSize = FileUtil.formatSize(sumSize); String time = DateExtensionUtil.getIntervalForView(beginDate, new Date()); LOGGER.debug("Write data over,sumSize:[{}],bufferLength:[{}],loopCount:[{}],time:{}", formatSize, bufferLength, loopCount, time); } } catch (IOException e) { throw new UncheckedIOException(e); } finally { IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(writableByteChannel); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(readableByteChannel); } }
From source file:org.eclipse.thym.core.internal.util.FileUtils.java
private static void createFileFromZipFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException { if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { throw new IOException("Can not create parent directory for " + file.toString()); }/*from w w w . j a v a2s.com*/ file.createNewFile(); FileOutputStream fout = null; FileChannel out = null; InputStream in = null; try { fout = new FileOutputStream(file); out = fout.getChannel(); in = zipFile.getInputStream(zipEntry); out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE); } finally { if (out != null) out.close(); if (in != null) in.close(); if (fout != null) fout.close(); } }
From source file:org.alfresco.contentstore.ContentStoreTest.java
@Test public void test2() throws Exception { UserContext.setUser("user1"); checksumService.setBlockSize(1024 * 8); // 8K String nodeId = GUID.generate(); long nodeVersion = 1l; MimeType mimeType = MimeType.XLSX; final Node node = Node.build().nodeId(nodeId).nodeVersion(nodeVersion).mimeType(mimeType); try (InputStream in = getClass().getClassLoader().getResourceAsStream("test.xlsx"); OutputStream out = contentStore.getWriter(node).getOutputStream()) { time(new Task<Void>() { @Override//from www. j a v a2 s . co m public String message() { return "Copy test.xlsx to Cassandra content store"; } @Override public Void execute() throws Exception { IOUtils.copy(in, out); return null; } }); } assertFileEquals(time(new Task<InputStream>() { @Override public String message() { return "Get test.xlsx from local file store"; } @Override public InputStream execute() throws Exception { return getClass().getClassLoader().getResourceAsStream("test.xlsx"); } }), time(new Task<InputStream>() { @Override public String message() { return "Get test.xlsx from Cassandra content store"; } @Override public InputStream execute() throws Exception { return contentStore.getReader(node).getStream(); } }), new State()); try (ReadableByteChannel channel1 = Channels .newChannel(getClass().getClassLoader().getResourceAsStream("test1.xlsx"))) { PatchDocument patchDocument = getPatch(node, channel1); long start = System.nanoTime(); contentStore.applyPatch(node, patchDocument); long end = System.nanoTime(); System.out.println("Patch applied time = " + (end - start) / 1000000 + "ms"); Node node1 = node.nodeVersion(node.getNodeVersion() + 1); ContentReader reader1 = contentStore.getReader(node1); try (InputStream in3 = getClass().getClassLoader().getResourceAsStream("test1.xlsx"); InputStream in4 = reader1.getStream()) { assertFileEquals(in3, in4, new State()); } } finally { UserContext.clearUser(); } }
From source file:org.getspout.spout.Spout.java
protected void update() { //test install once File runOnce = new File(getDataFolder(), "runonce"); if (!runOnce.exists()) { try {/*ww w. j a v a 2 s . c o m*/ runOnce.createNewFile(); pingLink("http://bit.ly/spoutserverrunonce"); } catch (Exception e) { } } if (!isUpdateAvailable()) { return; } FileOutputStream fos = null; try { File directory = new File(Bukkit.getServer().getUpdateFolder()); if (!directory.exists()) { try { directory.mkdir(); } catch (SecurityException e1) { } } File tempDirectory = new File(directory, "temp"); if (!tempDirectory.exists()) { try { tempDirectory.mkdir(); } catch (SecurityException e1) { } } File plugin = new File(directory.getPath(), "Spout.jar"); File temp = new File(tempDirectory.getPath(), "Spout.jar"); if (!plugin.exists()) { URL spout = new URL( "http://ci.getspout.org/view/SpoutDev/job/Spout/promotion/latest/Recommended/artifact/target/spout-dev-SNAPSHOT.jar"); HttpURLConnection con = (HttpURLConnection) (spout.openConnection()); System.setProperty("http.agent", ""); //Spoofing the user agent is required to track stats con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30"); ReadableByteChannel rbc = Channels.newChannel(con.getInputStream()); fos = new FileOutputStream(temp); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } if (temp.exists()) { FileUtils.moveFile(temp, plugin); } } catch (Exception e) { } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { } } } }
From source file:com.meltmedia.cadmium.core.FileSystemManager.java
public static void streamCopy(InputStream streamIn, OutputStream streamOut, boolean leaveOutputOpen) throws IOException { ReadableByteChannel input = Channels.newChannel(streamIn); WritableByteChannel output = Channels.newChannel(streamOut); ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (input.read(buffer) != -1) { buffer.flip();//from w w w.j a v a2 s . com output.write(buffer); buffer.compact(); } buffer.flip(); // Make sure the buffer is empty while (buffer.hasRemaining()) { output.write(buffer); } input.close(); if (!leaveOutputOpen) { output.close(); } }
From source file:org.apache.pulsar.functions.utils.Utils.java
public static NarClassLoader extractNarClassLoader(Path archivePath, String pkgUrl, File uploadedInputStreamFileName) { if (archivePath != null) { try {/*from w w w .ja v a 2 s . co m*/ return NarClassLoader.getFromArchive(archivePath.toFile(), Collections.emptySet()); } catch (IOException e) { throw new IllegalArgumentException(String.format("The archive %s is corrupted", archivePath)); } } if (!isEmpty(pkgUrl)) { if (pkgUrl.startsWith(org.apache.pulsar.common.functions.Utils.FILE)) { try { URL url = new URL(pkgUrl); File file = new File(url.toURI()); if (!file.exists()) { throw new IOException(pkgUrl + " does not exists locally"); } return NarClassLoader.getFromArchive(file, Collections.emptySet()); } catch (Exception e) { throw new IllegalArgumentException( "Corrupt User PackageFile " + pkgUrl + " with error " + e.getMessage()); } } else if (pkgUrl.startsWith("http")) { try { URL website = new URL(pkgUrl); File tempFile = File.createTempFile("function", ".tmp"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); try (FileOutputStream fos = new FileOutputStream(tempFile)) { fos.getChannel().transferFrom(rbc, 0, 10); } if (tempFile.exists()) { tempFile.delete(); } return NarClassLoader.getFromArchive(tempFile, Collections.emptySet()); } catch (Exception e) { throw new IllegalArgumentException( "Corrupt User PackageFile " + pkgUrl + " with error " + e.getMessage()); } } else { throw new IllegalArgumentException( "Unsupported url protocol " + pkgUrl + ", supported url protocols: [file/http/https]"); } } if (uploadedInputStreamFileName != null) { try { return NarClassLoader.getFromArchive(uploadedInputStreamFileName, Collections.emptySet()); } catch (IOException e) { throw new IllegalArgumentException(e.getMessage()); } } return null; }
From source file:backtype.storm.utils.Utils.java
public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException { WritableByteChannel out = null; NimbusClient client = null;/* ww w . j a v a 2 s.com*/ try { client = NimbusClient.getConfiguredClient(conf, 10 * 1000); String id = client.getClient().beginFileDownload(file); out = Channels.newChannel(new FileOutputStream(localFile)); while (true) { ByteBuffer chunk = client.getClient().downloadChunk(id); int written = out.write(chunk); if (written == 0) { client.getClient().finishFileDownload(id); break; } } } finally { if (out != null) out.close(); if (client != null) client.close(); } }
From source file:org.alfresco.contentstore.ContentStoreTest.java
@Test public void test3() throws IOException { checksumService.setBlockSize(8192);//w w w . j a va 2s .c o m UserContext.setUser("user1"); File f = copy("marbles-uncompressed.tif"); Node node = Node.build().nodeId(GUID.generate()).nodeVersion(1l); 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-uncompressed.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 in3 = getClass().getClassLoader().getResourceAsStream("marbles-uncompressed.tif"); InputStream in4 = new FileInputStream(f)) { assertFileEquals(in3, in4, state); } }
From source file:org.apache.jackrabbit.oak.plugins.segment.Segment.java
/** * Writes this segment to the given output stream. * * @param stream stream to which this segment will be written * @throws IOException on an IO error//from ww w . j av a 2s .c o m */ public void writeTo(OutputStream stream) throws IOException { ByteBuffer buffer = data.duplicate(); WritableByteChannel channel = Channels.newChannel(stream); while (buffer.hasRemaining()) { channel.write(buffer); } }
From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java
/** * ??/*ww w . j av a2 s. c o m*/ * * @param is * @return * @throws IOException */ public static String readStreamAsStr(InputStream is) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); WritableByteChannel dest = Channels.newChannel(bos); ReadableByteChannel src = Channels.newChannel(is); ByteBuffer bb = ByteBuffer.allocate(4096); while (src.read(bb) != -1) { bb.flip(); dest.write(bb); bb.clear(); } src.close(); dest.close(); return new String(bos.toByteArray(), Constants.ENCODING); }