当前位置: 首页>>代码示例>>Java>>正文


Java ReadableByteChannel.close方法代码示例

本文整理汇总了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));
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:27,代码来源:ByteStreamsTest.java

示例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);
}
 
开发者ID:linkingli,项目名称:FaceDistinguish,代码行数:24,代码来源:HttpUtil.java

示例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;
    }
}
 
开发者ID:Ptrk25,项目名称:CDN-FX-2.2,代码行数:21,代码来源:XMLUpdater.java

示例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;
    }
}
 
开发者ID:Ptrk25,项目名称:CDN-FX-2.2,代码行数:20,代码来源:Updater.java

示例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;
    }
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:28,代码来源:BitmapHelper.java

示例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();
}
 
开发者ID:LeshaInc,项目名称:openarch,代码行数:36,代码来源:Native.java

示例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());
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:33,代码来源:AbstractReadOnlyContentStoreTest.java

示例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();
}
 
开发者ID:networknt,项目名称:light-codegen,代码行数:9,代码来源:Utils.java

示例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();
}
 
开发者ID:PizzaCrust,项目名称:IodineToolkit,代码行数:8,代码来源:Main.java

示例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();
}
 
开发者ID:PizzaCrust,项目名称:IodineToolkit,代码行数:9,代码来源:Main.java

示例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();
}
 
开发者ID:Gmentsik,项目名称:ranch2git,代码行数:10,代码来源:FileAgent.java

示例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();
	}
}
 
开发者ID:GigaGamma,项目名称:McLink,代码行数:12,代码来源:UpdateFetcher.java

示例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();
            }
        }
    }
}
 
开发者ID:Ptrk25,项目名称:CDN-FX-2.2,代码行数:32,代码来源:XMLHandler.java

示例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;
}
 
开发者ID:Guerra24,项目名称:NanoUI,代码行数:57,代码来源:ResourceLoader.java


注:本文中的java.nio.channels.ReadableByteChannel.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。