本文整理匯總了Java中java.nio.channels.ReadableByteChannel.close方法的典型用法代碼示例。如果您正苦於以下問題:Java ReadableByteChannel.close方法的具體用法?Java ReadableByteChannel.close怎麽用?Java ReadableByteChannel.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.ReadableByteChannel
的用法示例。
在下文中一共展示了ReadableByteChannel.close方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testCopyFileChannel
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public void testCopyFileChannel() throws IOException {
final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size
ByteArrayOutputStream out = new ByteArrayOutputStream();
WritableByteChannel outChannel = Channels.newChannel(out);
File testFile = createTempFile();
FileOutputStream fos = new FileOutputStream(testFile);
byte[] dummyData = newPreFilledByteArray(chunkSize);
try {
for (int i = 0; i < 500; i++) {
fos.write(dummyData);
}
} finally {
fos.close();
}
ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel();
try {
ByteStreams.copy(inChannel, outChannel);
} finally {
inChannel.close();
}
byte[] actual = out.toByteArray();
for (int i = 0; i < 500 * chunkSize; i += chunkSize) {
assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize));
}
}
示例2: readStreamAsStr
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
/**
* 將流轉換為字符串
*
* @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);
}
示例3: update
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public boolean update(){
try{
DebugLogger.log("Updating XML...", Level.INFO);
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
path = path.substring(1, path.lastIndexOf("/")) + "/";
ReadableByteChannel in = Channels.newChannel(new URL(updateURL + "community.xml").openStream());
FileChannel out = new FileOutputStream((path+"community.xml")).getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
in.close();
out.close();
DebugLogger.log("XML Update successful!", Level.INFO);
return true;
}catch(Exception e){
DebugLogger.log("XML Update failed!", Level.INFO);
return false;
}
}
示例4: update
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public boolean update(){
try{
DebugLogger.log("Updating...", Level.INFO);
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
ReadableByteChannel in = Channels.newChannel(new URL(updateURL + "gfx.jar").openStream());
FileChannel out = new FileOutputStream(path).getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
in.close();
out.close();
DebugLogger.log("Update successful!", Level.INFO);
return true;
}catch(Exception e){
DebugLogger.log("Update failed!", Level.INFO);
return false;
}
}
示例5: compressBitmap
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
/**
* 壓縮某個輸入流中的圖片,可以解決網絡輸入流壓縮問題,並得到圖片對象
*
* @return Bitmap {@link Bitmap}
*/
public final static Bitmap compressBitmap(InputStream is, int reqsW, int reqsH) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ReadableByteChannel channel = Channels.newChannel(is);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) baos.write(buffer.get());
buffer.clear();
}
byte[] bts = baos.toByteArray();
Bitmap bitmap = compressBitmap(bts, reqsW, reqsH);
is.close();
channel.close();
baos.close();
return bitmap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
示例6: addLibrary
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public void addLibrary(String name) throws Exception {
OpenArch.log.info(String.format("Loading %s.", name));
String libPath = String.format("/assets/%s/lib/%s", OpenArch.MODID, name);
URL libUrl = getClass().getResource(libPath);
ReadableByteChannel in = Channels.newChannel(libUrl.openStream());
String tmpLibPath = String.format("%s/%s", tmpDir, name);
File tmpLibFile = new File(tmpLibPath);
if (tmpLibFile.exists()) {
return;
}
FileChannel out = new FileOutputStream(tmpLibFile).getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
tmpLibFile.deleteOnExit();
if (!tmpLibFile.setReadable(true, false)) {
throw new Exception("Failed to set readable flag on the library");
}
if (!tmpLibFile.setWritable(true, false)) {
throw new Exception("Failed to set writable flag on the library");
}
if (!tmpLibFile.setExecutable(true, false)) {
throw new Exception("Failed to set executable flag on the library");
}
out.close();
in.close();
}
示例7: testGetReaderForExistingContentUrl
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
/**
* Checks that the various methods of obtaining a reader are supported.
*/
@Test
public void testGetReaderForExistingContentUrl() throws Exception
{
ContentStore store = getStore();
String contentUrl = getExistingContentUrl();
if (contentUrl == null)
{
logger.warn("Store test testGetReaderForExistingContentUrl not possible on " + store.getClass().getName());
return;
}
// Get the reader
assertTrue("URL returned in set seems to no longer exist", store.exists(contentUrl));
ContentReader reader = store.getReader(contentUrl);
assertNotNull("Reader should never be null", reader);
assertTrue("Reader says content doesn't exist", reader.exists());
assertFalse("Reader should not be closed before a read", reader.isClosed());
assertFalse("The reader channel should not be open yet", reader.isChannelOpen());
// Open the channel
ReadableByteChannel readChannel = reader.getReadableChannel();
readChannel.read(ByteBuffer.wrap(new byte[500]));
assertFalse("Reader should not be closed during a read", reader.isClosed());
assertTrue("The reader channel should be open during a read", reader.isChannelOpen());
// Close the channel
readChannel.close();
assertTrue("Reader should be closed after a read", reader.isClosed());
assertFalse("The reader channel should be closed after a read", reader.isChannelOpen());
}
示例8: downloadUsingNIO
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
示例9: copy
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
private static void copy(File file, File file1) throws Exception {
ReadableByteChannel ori = Channels.newChannel(new FileInputStream(file));
FileOutputStream fileOutputStream = new FileOutputStream(file1);
fileOutputStream.getChannel().transferFrom(ori, 0, Long.MAX_VALUE);
ori.close();
fileOutputStream.close();
}
示例10: download
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
private static void download(String url, File output) throws Exception {
URL urlObject = new URL(url);
ReadableByteChannel readableByteChannel = Channels.newChannel(urlObject.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(output);
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
readableByteChannel.close();
fileOutputStream.close();
}
示例11: downloadFromWebResource
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public static long downloadFromWebResource(String webURL, String destinationPath) throws IOException {
URL website = new URL(webURL);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destinationPath);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
rbc.close();
fos.close();
return new File(destinationPath).length();
}
示例12: download
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public static void download(URL url, File file) {
try {
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
rbc.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: getXMLFileFromServer
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
public void getXMLFileFromServer()
{
if(PropertiesHandler.getProperties("disable3dsdbxml") != null)
{
if(PropertiesHandler.getProperties("disable3dsdbxml").equals("no"))
{
try
{
DebugLogger.log("INIT: Downloading 3dsdb newest XML database...", Level.INFO);
File tempFile = File.createTempFile("3dsdb", Long.toString(System.nanoTime()));
ReadableByteChannel in = Channels.newChannel(new URL("http://3dsdb.com/xml.php").openStream());
FileOutputStream fos = new FileOutputStream(tempFile);
FileChannel out = fos.getChannel();
out.transferFrom(in, 0L, Long.MAX_VALUE);
in.close();
out.close();
fos.close();
this.pathToDB = tempFile.getPath();
DebugLogger.log("INIT: Download completed!", Level.INFO);
}
catch (Exception e)
{
DebugLogger.log("INIT: Download failed! Using local file", Level.WARNING);
getXMLFileFromJar();
}
}
}
}
示例14: ioResourceToByteBuffer
import java.nio.channels.ReadableByteChannel; //導入方法依賴的package包/類
/**
* Reads the specified resource and returns the raw data as a ByteBuffer.
*
* @param resource
* the resource to read
* @param bufferSize
* the initial buffer size
*
* @return the resource data
*
* @throws IOException
* if an IO error occurs
*/
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
ByteBuffer buffer;
File file = new File(resource);
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
buffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
while (fc.read(buffer) != -1)
;
fis.close();
fc.close();
} else {
buffer = BufferUtils.createByteBuffer(bufferSize);
InputStream source = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (source == null)
throw new FileNotFoundException(resource);
try {
ReadableByteChannel rbc = Channels.newChannel(source);
try {
while (true) {
int bytes = rbc.read(buffer);
if (bytes == -1)
break;
if (buffer.remaining() == 0)
buffer = resizeBuffer(buffer, buffer.capacity() * 2);
}
} finally {
rbc.close();
}
} finally {
source.close();
}
}
buffer.put((byte) 0);
buffer.flip();
return buffer;
}