Java Code Examples for java.nio.channels.ReadableByteChannel#close()

The following examples show how to use java.nio.channels.ReadableByteChannel#close() . 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: PathResourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getReadableByteChannel() throws IOException {
	PathResource resource = new PathResource(TEST_FILE);
	ReadableByteChannel channel = null;
	try {
		channel = resource.readableChannel();
		ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength());
		channel.read(buffer);
		buffer.rewind();
		assertThat(buffer.limit(), greaterThan(0));
	}
	finally {
		if (channel != null) {
			channel.close();
		}
	}
}
 
Example 2
Source File: DownloadSample.java    From huaweicloud-sdk-java-obs with Apache License 2.0 6 votes vote down vote up
private static void downloadToLocalFile()
    throws ObsException, IOException
{
    ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null);
    ReadableByteChannel rchannel = Channels.newChannel(obsObject.getObjectContent());
    
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    WritableByteChannel wchannel = Channels.newChannel(new FileOutputStream(new File(localFilePath)));
    
    while (rchannel.read(buffer) != -1)
    {
        buffer.flip();
        wchannel.write(buffer);
        buffer.clear();
    }
    rchannel.close();
    wchannel.close();
}
 
Example 3
Source File: MavenWrapperDownloader.java    From blog-tutorials with MIT License 5 votes vote down vote up
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 4
Source File: MavenWrapperDownloader.java    From istio-fleetman with MIT License 5 votes vote down vote up
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 5
Source File: MavenWrapperDownloader.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
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 6
Source File: MavenWrapperDownloader.java    From rsocket-routing-sample with Apache License 2.0 5 votes vote down vote up
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 7
Source File: MavenWrapperDownloader.java    From springcloud_for_noob with MIT License 5 votes vote down vote up
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 8
Source File: MavenWrapperDownloader.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
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 9
Source File: MavenWrapperDownloader.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
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 10
Source File: MavenWrapperDownloader.java    From yugastore-java with Apache License 2.0 5 votes vote down vote up
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 11
Source File: MavenWrapperDownloader.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
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 12
Source File: MavenWrapperDownloader.java    From bootiful-testing-online-training with Apache License 2.0 5 votes vote down vote up
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 13
Source File: IO.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(ReadableByteChannel channel) {
  try {
    if (channel != null) {
      channel.close();
    }
  } catch (IOException ioe) {
    throw new IllegalStateException(ioe);
  }
}
 
Example 14
Source File: MavenWrapperDownloader.java    From blog-tutorials with MIT License 5 votes vote down vote up
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: MavenWrapperDownloader.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
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 16
Source File: MavenWrapperDownloader.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
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 tutorials with MIT License 5 votes vote down vote up
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: S3FileSystemTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteAndRead() throws IOException {
  S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options(), client);

  client.createBucket("testbucket");

  byte[] writtenArray = new byte[] {0};
  ByteBuffer bb = ByteBuffer.allocate(writtenArray.length);
  bb.put(writtenArray);

  // First create an object and write data to it
  S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/foo/bar.txt");
  WritableByteChannel writableByteChannel =
      s3FileSystem.create(path, builder().setMimeType("application/text").build());
  writableByteChannel.write(bb);
  writableByteChannel.close();

  // Now read the same object
  ByteBuffer bb2 = ByteBuffer.allocate(writtenArray.length);
  ReadableByteChannel open = s3FileSystem.open(path);
  open.read(bb2);

  // And compare the content with the one that was written
  byte[] readArray = bb2.array();
  assertArrayEquals(readArray, writtenArray);
  open.close();
}
 
Example 19
Source File: AbstractStreamCoasterChannel.java    From swift-k with Apache License 2.0 4 votes vote down vote up
protected void stepNIO() throws IOException {
	ReadableByteChannel channel = (ReadableByteChannel) getNIOChannel();
	if (state == STATE_IDLE) {
		readFromChannel(channel, bhdr);
		if (!bhdr.hasRemaining()) {
			tag = unpack(rhdr, 0);
			flags = unpack(rhdr, 4);
			len = unpack(rhdr, 8);
			hcsum = unpack(rhdr, 12);
			if (logger.isDebugEnabled()) {
				logger.debug("NIOrecv channel: "+ this + ", tag: " + tag + ", flags: " + flags + ", len: " + len);
			}
			if ((tag ^ flags ^ len) != hcsum) {
				logger.warn("(NIO) Header checksum failed. Computed checksum: " + 
						Integer.toHexString(tag ^ flags ^ len) + 
						", checksum: " + Integer.toHexString(hcsum));
				logger.warn("Tag: " + tag + ", flags: " + flags + ", len: " + len + ", data: " + ppByteBuf(rhdr));
				channel.close();
				throw new IOException("Header checksum failed");
			}
			csum = unpack(rhdr, 16);
			if (len > 1048576) {
				logger.warn("Big len: " + len + " (tag: " + tag + ", flags: " + flags + ")");
				bdata = ByteBuffer.wrap(data = new byte[1024]);
				readFromChannel(channel, bdata);
				logger.warn("data: " + ppByteBuf(data));
				return;
			}
			bdata = ByteBuffer.wrap(data = new byte[len]);
			state = STATE_RECEIVING_DATA;
			bhdr.rewind();
		}
	}
	if (state == STATE_RECEIVING_DATA) {
		readFromChannel(channel, bdata);
		
		if (!bdata.hasRemaining()) {
			state = STATE_IDLE;
			
			if (csum != 0) {
				Adler32 c = new Adler32();
				c.update(data);
				
				if (((int) c.getValue()) != csum) {
					logger.warn("Data checksum failed. Compute checksum: " + 
							Integer.toHexString((int) c.getValue()) + ", checksum: " + Integer.toHexString(csum));
				}
			}
			byte[] tdata = data;
			// don't hold reference from the channel to the data
			data = null; bdata = null;
			if (flagIsSet(flags, REPLY_FLAG)) {
				// reply
				handleReply(tag, flags, len, tdata);
			}
			else {
				// request
				handleRequest(tag, flags, len, tdata);
			}
		}
	}
}
 
Example 20
Source File: NPOIFSFileSystem.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a POIFSFileSystem from an <tt>InputStream</tt>.  Normally the stream is read until
 * EOF.  The stream is always closed.<p>
 *
 * Some streams are usable after reaching EOF (typically those that return <code>true</code>
 * for <tt>markSupported()</tt>).  In the unlikely case that the caller has such a stream
 * <i>and</i> needs to use it after this constructor completes, a work around is to wrap the
 * stream in order to trap the <tt>close()</tt> call.  A convenience method (
 * <tt>createNonClosingInputStream()</tt>) has been provided for this purpose:
 * <pre>
 * InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is);
 * HSSFWorkbook wb = new HSSFWorkbook(wrappedStream);
 * is.reset();
 * doSomethingElse(is);
 * </pre>
 * Note also the special case of <tt>ByteArrayInputStream</tt> for which the <tt>close()</tt>
 * method does nothing.
 * <pre>
 * ByteArrayInputStream bais = ...
 * HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() !
 * bais.reset(); // no problem
 * doSomethingElse(bais);
 * </pre>
 *
 * @param stream the InputStream from which to read the data
 *
 * @exception IOException on errors reading, or on invalid data
 */

public NPOIFSFileSystem(InputStream stream)
    throws IOException
{
    this(false);
    
    ReadableByteChannel channel = null;
    boolean success = false;
    
    try {
       // Turn our InputStream into something NIO based
       channel = Channels.newChannel(stream);
       
       // Get the header
       ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
       IOUtils.readFully(channel, headerBuffer);
       
       // Have the header processed
       _header = new HeaderBlock(headerBuffer);
       
       // Sanity check the block count
       BlockAllocationTableReader.sanityCheckBlockCount(_header.getBATCount());
   
       // We need to buffer the whole file into memory when
       //  working with an InputStream.
       // The max possible size is when each BAT block entry is used
       long maxSize = BATBlock.calculateMaximumSize(_header); 
       if (maxSize > Integer.MAX_VALUE) {
           throw new IllegalArgumentException("Unable read a >2gb file via an InputStream");
       }
       ByteBuffer data = ByteBuffer.allocate((int)maxSize);
       
       // Copy in the header
       headerBuffer.position(0);
       data.put(headerBuffer);
       data.position(headerBuffer.capacity());
       
       // Now read the rest of the stream
       IOUtils.readFully(channel, data);
       success = true;
       
       // Turn it into a DataSource
       _data = new ByteArrayBackedDataSource(data.array(), data.position());
    } finally {
       // As per the constructor contract, always close the stream
       if(channel != null)
          channel.close();
       closeInputStream(stream, success);
    }
    
    // Now process the various entries
    readCoreContents();
}