當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。