Add Resource.readableChannel()
Added readableChannel() to Resource, which returns a java.nio.ReadableByteChannel. The default implementation uses Channels.newChannel() to create a channel based on what is returned from getInputStream(). Subclasses have more effecient, file-based implementations. Issue: SPR-14698
This commit is contained in:
committed by
Rossen Stoyanchev
parent
06395f41cb
commit
c6a61e0d85
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,9 @@ package org.springframework.core.io;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -42,6 +45,7 @@ import static org.mockito.BDDMockito.*;
|
||||
* @author Nicholas Williams
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class PathResourceTests {
|
||||
|
||||
@@ -287,4 +291,36 @@ public class PathResourceTests {
|
||||
resource.getOutputStream();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReadableByteChannel() throws Exception {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReadableByteChannelForDir() throws Exception {
|
||||
PathResource resource = new PathResource(TEST_DIR);
|
||||
thrown.expect(NoSuchFileException.class);
|
||||
resource.readableChannel();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReadableByteChannelDoesNotExist() throws Exception {
|
||||
PathResource resource = new PathResource(NON_EXISTING_FILE);
|
||||
thrown.expect(NoSuchFileException.class);
|
||||
resource.readableChannel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Ignore;
|
||||
@@ -254,4 +256,22 @@ public class ResourceTests {
|
||||
resource.contentLength();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetReadableByteChannel() throws IOException {
|
||||
Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile());
|
||||
ReadableByteChannel channel = null;
|
||||
try {
|
||||
channel = resource.readableChannel();
|
||||
ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength());
|
||||
channel.read(buffer);
|
||||
buffer.rewind();
|
||||
assertTrue(buffer.limit() > 0);
|
||||
}
|
||||
finally {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user