java.nio.channels.ReadableByteChannel Java Examples
The following examples show how to use
java.nio.channels.ReadableByteChannel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpUtil.java From neembuu-uploader with GNU General Public License v3.0 | 6 votes |
private static ReadableByteChannel wrap(final InputStream is, final HttpEntity he, final Content c, final double contentLength){ return new ReadableByteChannel() { double total = 0; @Override public int read(ByteBuffer dst) throws IOException { byte[]b=new byte[dst.capacity()]; int r = is.read(b); //this sleep is just to slow down update to see, if the UI is working or not ! // NU's update is very very very fast //try{Thread.sleep(1000);}catch(Exception a){} dst.put(b,0,r); total+=r; c.setProgress(total/contentLength); return r; } @Override public boolean isOpen() { return he.isStreaming(); } @Override public void close() throws IOException { is.close(); } }; }
Example #2
Source File: ChannelTest.java From yajsync with GNU General Public License v3.0 | 6 votes |
@Test public void testTransferSingleMinInt() throws ChannelException { ByteBuffer wb = ByteBuffer.allocate(8); WritableByteBufferChannel w = new WritableByteBufferChannel(wb); RsyncOutChannel _out = new RsyncOutChannel(w); int testInt = Integer.MIN_VALUE; _out.putInt(testInt); _out.flush(); assertTrue(wb.position() > 0); wb.flip(); ReadableByteChannel r = new ReadableByteBufferChannel(wb); RsyncInChannel _in = new RsyncInChannel(r, this); int resultInt = _in.getInt(); assertTrue(wb.position() > 0); assertTrue(resultInt == testInt); }
Example #3
Source File: ClientNetworkReceiver.java From riiablo with Apache License 2.0 | 6 votes |
@Override protected void processSystem() { InputStream in = socket.getInputStream(); try { if (in.available() > 0) { ReadableByteChannel channel = Channels.newChannel(in); buffer.clear(); int i = channel.read(buffer); buffer.rewind().limit(i); D2GS d2gs = new D2GS(); int p = 0; while (buffer.hasRemaining()) { int size = ByteBufferUtil.getSizePrefix(buffer); D2GS.getRootAsD2GS(ByteBufferUtil.removeSizePrefix(buffer), d2gs); if (DEBUG_PACKET) Gdx.app.debug(TAG, p++ + " packet type " + D2GSData.name(d2gs.dataType()) + ":" + size + "B"); process(d2gs); // System.out.println(buffer.position() + "->" + (buffer.position() + size + 4)); buffer.position(buffer.position() + size + 4); // advance position passed current packet + size prefix of next packet } } } catch (Throwable t) { Gdx.app.error(TAG, t.getMessage(), t); } }
Example #4
Source File: RestJSONResponseParser.java From netbeans with Apache License 2.0 | 6 votes |
/** * Copy all data from <code>in</code> {@link InputStream} * into <code>out</code> {@link OutputStream}. * <p/> * @param in Source {@link InputStream} to read all data. * @param out Target {@link OutputStream} to write all data. * @throws IOException when there is a problem with copying data. */ public static void copy(InputStream in, OutputStream out) throws IOException { try { ReadableByteChannel inChannel = Channels.newChannel(in); WritableByteChannel outChannel = Channels.newChannel(out); ByteBuffer byteBuffer = ByteBuffer.allocate(10240); int read; do { read = inChannel.read(byteBuffer); if (read > 0) { byteBuffer.limit(byteBuffer.position()); byteBuffer.rewind(); outChannel.write(byteBuffer); byteBuffer.clear(); } } while (read != -1); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }
Example #5
Source File: ReadOffset.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ReadableByteChannel rbc = new ReadableByteChannel() { public int read(ByteBuffer dst) { dst.put((byte)0); return 1; } public boolean isOpen() { return true; } public void close() { } }; InputStream in = Channels.newInputStream(rbc); byte[] b = new byte[3]; in.read(b, 0, 1); in.read(b, 2, 1); // throws IAE }
Example #6
Source File: Util.java From AsyncSocket with MIT License | 6 votes |
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip(); // write to the channel, may block dest.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing recycle() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { dest.write(buffer); } }
Example #7
Source File: ReadByte.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ReadableByteChannel channel = new ReadableByteChannel() { public int read(ByteBuffer dst) { dst.put((byte) 129); return 1; } public boolean isOpen() { return true; } public void close() { } }; InputStream in = Channels.newInputStream(channel); int data = in.read(); if (data < 0) throw new RuntimeException( "InputStream.read() spec'd to return 0-255"); }
Example #8
Source File: NonResumableDriveUploader.java From urltodrive with MIT License | 6 votes |
private void downloadFile() throws IOException { String random = generateRandomString(); File downloadDirectory = new File(System.getenv("TEMP_STROAGE"), random); boolean created = downloadDirectory.mkdir(); if (!created) { uploadInformation.setUploadStatus(UploadStatus.failed); uploadInformation.setErrorMessage("Can not create download directory."); throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, "Can not create download directory."); } downloadedFile = new File(downloadDirectory, downloadFileInfo.getFileName()); ReadableByteChannel rbc = Channels.newChannel(downloadFileInfo.getUploadUrl().openStream()); FileOutputStream fos = new FileOutputStream(downloadedFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); }
Example #9
Source File: MP4Convert.java From arcusplatform with Apache License 2.0 | 6 votes |
private static boolean fill(ByteBuffer buffer, ReadableByteChannel input) throws IOException { buffer.compact(); while (true) { int read = input.read(buffer); int size = buffer.position(); if (size >= PACKET_SIZE) { buffer.flip(); return true; } if (read < 0) { buffer.flip(); return false; } } }
Example #10
Source File: BamSlicerApplication.java From hmftools with GNU General Public License v3.0 | 6 votes |
@NotNull private static File downloadIndex(@NotNull URL indexUrl) throws IOException { LOGGER.info("Downloading index from {}", indexUrl); ReadableByteChannel indexChannel = Channels.newChannel(indexUrl.openStream()); String extension = ".bai"; if (indexUrl.getPath().contains(".crai")) { LOGGER.debug("Located crai from the index on {}", indexUrl); extension = ".crai"; } File index = File.createTempFile("tmp", extension); FileOutputStream indexOutputStream = new FileOutputStream(index); indexOutputStream.getChannel().transferFrom(indexChannel, 0, Long.MAX_VALUE); indexOutputStream.close(); indexChannel.close(); LOGGER.info("Downloaded index to {}", index.getPath()); return index; }
Example #11
Source File: ReadByte.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ReadableByteChannel channel = new ReadableByteChannel() { public int read(ByteBuffer dst) { dst.put((byte) 129); return 1; } public boolean isOpen() { return true; } public void close() { } }; InputStream in = Channels.newInputStream(channel); int data = in.read(); if (data < 0) throw new RuntimeException( "InputStream.read() spec'd to return 0-255"); }
Example #12
Source File: MultiByteBuff.java From hbase with Apache License 2.0 | 6 votes |
private int internalRead(ReadableByteChannel channel, long offset, ChannelReader reader) throws IOException { checkRefCount(); int total = 0; while (buffsIterator.hasNext()) { ByteBuffer buffer = buffsIterator.next(); int len = read(channel, buffer, offset, reader); if (len > 0) { total += len; offset += len; } if (buffer.hasRemaining()) { break; } } return total; }
Example #13
Source File: CsvToAvro.java From java-docs-samples with Apache License 2.0 | 6 votes |
public static String getSchema(String schemaPath) throws IOException { ReadableByteChannel chan = FileSystems.open(FileSystems.matchNewResource( schemaPath, false)); try (InputStream stream = Channels.newInputStream(chan)) { BufferedReader streamReader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); StringBuilder dataBuilder = new StringBuilder(); String line; while ((line = streamReader.readLine()) != null) { dataBuilder.append(line); } return dataBuilder.toString(); } }
Example #14
Source File: MavenWrapperDownloader.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #15
Source File: Sender.java From yajsync with GNU General Public License v3.0 | 5 votes |
public static Builder newServer(ReadableByteChannel in, WritableByteChannel out, Iterable<Path> sourceFiles, byte[] checksumSeed) { Builder builder = new Builder(in, out, sourceFiles, checksumSeed); builder._isSendStatistics = true; builder._isExitEarlyIfEmptyList = true; builder._isExitAfterEOF = false; return builder; }
Example #16
Source File: MavenWrapperDownloader.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #17
Source File: MavenWrapperDownloader.java From springcloud_for_noob with MIT License | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #18
Source File: SequencerCollector.java From repairnator with MIT License | 5 votes |
protected void saveFileDiff(String slug, String sha) throws IOException { System.out.println("saving file..."); URL url = new URL("https", "github.com", "/" + slug + "/commit/" + sha + ".diff"); ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); File f = new File(diffsPath + "/" + slug.replace("/", "-") + "-" + sha + "/commit.diff"); f.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(f, false); fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); fileOutputStream.close(); }
Example #19
Source File: ByteBufferUtils.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
/** * This method rewrites bytes from input stream to output stream * * @param in * @param out * @throws IOException */ public static void rewrite(InputStream in, OutputStream out) throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(Defaults.DEFAULT_INTERNAL_IO_BUFFER_SIZE); ReadableByteChannel reader = Channels.newChannel(in); WritableByteChannel writer = Channels.newChannel(out); while (reload(buffer,reader) > 0){ flush(buffer, writer); } }
Example #20
Source File: ChannelBufferInputStream.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
public ChannelBufferInputStream(ReadableByteChannel channel, int bufferSize) throws IOException { super(channel); if (bufferSize <= 0) { throw new IllegalArgumentException("invalid bufferSize=" + bufferSize); } this.buffer = allocateBuffer(bufferSize); // flip to force refill on first use this.buffer.flip(); }
Example #21
Source File: MavenWrapperDownloader.java From ms-identity-java-webapp with MIT License | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #22
Source File: RandomAccessFileIO.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { long written = ch.transferFrom(src, position, count); if (written > 0) position(position + written); return written; }
Example #23
Source File: MavenWrapperDownloader.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #24
Source File: MavenWrapperDownloader.java From java-microservices-examples with Apache License 2.0 | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #25
Source File: StreamingDataGeneratorTest.java From DataflowTemplates with Apache License 2.0 | 5 votes |
/** * Helper to generate files for testing. * * @param filePath The path to the file to write. * @param fileContents The content to write. * @return The file written. * @throws IOException If an error occurs while creating or writing the file. */ private static ResourceId writeToFile(String filePath, String fileContents) throws IOException { ResourceId resourceId = FileSystems.matchNewResource(filePath, false); // Write the file contents to the channel and close. try (ReadableByteChannel readChannel = Channels.newChannel(new ByteArrayInputStream(fileContents.getBytes()))) { try (WritableByteChannel writeChannel = FileSystems.create(resourceId, MimeTypes.TEXT)) { ByteStreams.copy(readChannel, writeChannel); } } return resourceId; }
Example #26
Source File: WARCSpout.java From storm-crawler with Apache License 2.0 | 5 votes |
private static ReadableByteChannel openChannel(String path) throws IOException { if (path.matches("^https?://.*")) { URL warcUrl = new URL(path); return Channels.newChannel(warcUrl.openStream()); } else { return FileChannel.open(Paths.get(path)); } }
Example #27
Source File: MavenWrapperDownloader.java From axon-initializr with Apache License 2.0 | 5 votes |
private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); }
Example #28
Source File: ByteCharBuffer.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
/** * This method should be protected USE setSource() instead!!!!! * * @param reader */ public void setReader(ReadableByteChannel reader) { this.reader = reader; this.eof = false; init(); reset(); inited = true; }
Example #29
Source File: CodepointIteratorReadableByteChannel.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull private static ByteBuffer _convert (@Nonnull @WillClose final ReadableByteChannel aChannel) throws IOException { try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream (); final WritableByteChannel aOutChannel = Channels.newChannel (aBAOS)) { final ByteBuffer buf = ByteBuffer.allocate (1024); while (aChannel.read (buf) > 0) { buf.flip (); aOutChannel.write (buf); } return ByteBuffer.wrap (aBAOS.toByteArray ()); } }
Example #30
Source File: Ffmpeg.java From kurento-java with Apache License 2.0 | 5 votes |
public static float getPesqMos(String audio, int sampleRate) { float pesqmos = 0; try { String pesq = KurentoTest.getTestFilesDiskPath() + "/bin/pesq/PESQ"; String origWav = ""; if (audio.startsWith(HTTP_TEST_FILES)) { origWav = KurentoTest.getTestFilesDiskPath() + audio.replace(HTTP_TEST_FILES, ""); } else { // Download URL origWav = KurentoTest.getDefaultOutputFile("downloaded.wav"); URL url = new URL(audio); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(origWav); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } Shell.runAndWait(pesq, "+" + sampleRate, origWav, RECORDED_WAV); List<String> lines = FileUtils.readLines(new File(PESQ_RESULTS), "utf-8"); pesqmos = Float.parseFloat(lines.get(1).split("\t")[2].trim()); log.debug("PESQMOS " + pesqmos); Shell.runAndWait("rm", PESQ_RESULTS); } catch (IOException e) { log.error("Exception recording local audio", e); } return pesqmos; }