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


Java Channels.newChannel方法代码示例

本文整理汇总了Java中java.nio.channels.Channels.newChannel方法的典型用法代码示例。如果您正苦于以下问题:Java Channels.newChannel方法的具体用法?Java Channels.newChannel怎么用?Java Channels.newChannel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.Channels的用法示例。


在下文中一共展示了Channels.newChannel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: update

import java.nio.channels.Channels; //导入方法依赖的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

示例2: update

import java.nio.channels.Channels; //导入方法依赖的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

示例3: isStreamEqual

import java.nio.channels.Channels; //导入方法依赖的package包/类
private static boolean isStreamEqual(InputStream i1, InputStream i2)
        throws IOException {

    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);

    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

    try {
        while (true) {

            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);

            if (n1 == -1 || n2 == -1) return n1 == n2;

            buf1.flip();
            buf2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++)
                if (buf1.get() != buf2.get())
                    return false;

            buf1.compact();
            buf2.compact();
        }

    } finally {
        if (i1 != null) i1.close();
        if (i2 != null) i2.close();
    }
}
 
开发者ID:eclipse,项目名称:gemoc-studio,代码行数:34,代码来源:IFileUtils.java

示例4: testPacketizeChunks

import java.nio.channels.Channels; //导入方法依赖的package包/类
@Test
public void testPacketizeChunks() throws IOException {
    IPv4Packet originalPacket = new IPv4Packet(createMockPacket());
    IPv4Header ipv4Header = originalPacket.getIpv4Header();
    TransportHeader transportHeader = originalPacket.getTransportHeader();

    byte[] data = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88};
    ReadableByteChannel channel = Channels.newChannel(new ByteArrayInputStream(data));

    Packetizer packetizer = new Packetizer(ipv4Header, transportHeader);

    IPv4Packet packet = packetizer.packetize(channel, 2);
    ByteBuffer packetPayload = packet.getPayload();

    Assert.assertEquals(30, packet.getIpv4Header().getTotalLength());
    Assert.assertEquals(2, packetPayload.remaining());
    Assert.assertEquals(0x1122, Short.toUnsignedInt(packetPayload.getShort()));

    packet = packetizer.packetize(channel, 3);
    packetPayload = packet.getPayload();
    Assert.assertEquals(31, packet.getIpv4Header().getTotalLength());
    Assert.assertEquals(3, packetPayload.remaining());
    Assert.assertEquals(0x33, packetPayload.get());
    Assert.assertEquals(0x44, packetPayload.get());
    Assert.assertEquals(0x55, packetPayload.get());

    packet = packetizer.packetize(channel, 1024);
    packetPayload = packet.getPayload();
    Assert.assertEquals(31, packet.getIpv4Header().getTotalLength());
    Assert.assertEquals(3, packetPayload.remaining());
    Assert.assertEquals(0x66, packetPayload.get());
    Assert.assertEquals(0x77, packetPayload.get());
    Assert.assertEquals((byte) 0x88, packetPayload.get());
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:35,代码来源:PacketizerTest.java

示例5: download

import java.nio.channels.Channels; //导入方法依赖的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

示例6: setupActiveMqLibForTesting

import java.nio.channels.Channels; //导入方法依赖的package包/类
static String setupActiveMqLibForTesting(boolean clean) {
    String[] urlsStrings = new String[]{
            "http://central.maven.org/maven2/org/apache/activemq/activemq-client/5.13.0/activemq-client-5.13.0.jar",
            "http://central.maven.org/maven2/org/apache/activemq/activemq-broker/5.13.0/activemq-broker-5.13.0.jar",
            "http://central.maven.org/maven2/org/apache/geronimo/specs/geronimo-j2ee-management_1.0_spec/1.0.1/geronimo-j2ee-management_1.0_spec-1.0.1.jar",
            "http://central.maven.org/maven2/org/fusesource/hawtbuf/hawtbuf/1.11/hawtbuf-1.11.jar" };

    try {
        File activeMqLib = new File("target/active-mq-lib");
        if (activeMqLib.exists() && clean) {
            FileUtils.deleteDirectory(activeMqLib);
        }
        activeMqLib.mkdirs();
        for (String urlString : urlsStrings) {
            URL url = new URL(urlString);
            String path = url.getPath();
            path = path.substring(path.lastIndexOf("/") + 1);
            logger.info("Downloading: " + path);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            try (FileOutputStream fos = new FileOutputStream(new File(activeMqLib, path))) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                fos.close();
            }
        }
        return activeMqLib.getAbsolutePath();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to download ActiveMQ libraries.", e);
    }
}
 
开发者ID:lsac,项目名称:nifi-jms-jndi,代码行数:30,代码来源:TestUtils.java

示例7: addLibrary

import java.nio.channels.Channels; //导入方法依赖的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

示例8: testCopyChannel

import java.nio.channels.Channels; //导入方法依赖的package包/类
public void testCopyChannel() throws IOException {
  byte[] expected = newPreFilledByteArray(100);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  WritableByteChannel outChannel = Channels.newChannel(out);

  ReadableByteChannel inChannel =
      Channels.newChannel(new ByteArrayInputStream(expected));
  ByteStreams.copy(inChannel, outChannel);
  assertEquals(expected, out.toByteArray());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:11,代码来源:ByteStreamsTest.java

示例9: downloadFromWebResource

import java.nio.channels.Channels; //导入方法依赖的package包/类
public static long downloadFromWebResource(String webURL, String destinationPath, String authUser, String authPass) throws IOException {
    if(authUser != null && authPass != null){
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(authUser,authPass.toCharArray());}
        });
    }

    URL website = new URL(webURL);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destinationPath);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    return new File(destinationPath).length();
}
 
开发者ID:Gmentsik,项目名称:ranch2git,代码行数:14,代码来源:FileAgent.java

示例10: copy

import java.nio.channels.Channels; //导入方法依赖的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

示例11: _writeChildBoxes

import java.nio.channels.Channels; //导入方法依赖的package包/类
public void _writeChildBoxes(ByteBuffer bb) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    WritableByteChannel wbc = Channels.newChannel(baos);
    try {
        for (Box box : boxes) {
            box.getBox(wbc);
        }
        wbc.close();
    } catch (IOException e) {
        throw new RuntimeException("Cannot happen. Everything should be in memory and therefore no exceptions.");
    }
    bb.put(baos.toByteArray());
}
 
开发者ID:begeekmyfriend,项目名称:mp4parser_android,代码行数:14,代码来源:SampleEntry.java

示例12: testSimple

import java.nio.channels.Channels; //导入方法依赖的package包/类
@Test
public void testSimple() throws IOException {
    ByteBuffer buffer = createChunk();

    StreamBuffer streamBuffer = new StreamBuffer(9);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    WritableByteChannel channel = Channels.newChannel(bos);

    streamBuffer.readFrom(buffer);
    streamBuffer.writeTo(channel);

    byte[] result = bos.toByteArray();
    Assert.assertArrayEquals(buffer.array(), result);
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:15,代码来源:StreamBufferTest.java

示例13: getDirectReadableChannel

import java.nio.channels.Channels; //导入方法依赖的package包/类
@Override
protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException
{
    try
    {
        // the file must exist
        if (!file.exists())
        {
            throw new IOException("File does not exist: " + file);
        }
        // create the channel
        ReadableByteChannel channel = null;
        if (allowRandomAccess)
        {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");  // won't create it
            channel = randomAccessFile.getChannel();
        }
        else
        {
            InputStream is = new FileInputStream(file);
            channel = Channels.newChannel(is);
        }
        // done
        if (logger.isDebugEnabled())
        {
            logger.debug("Opened read channel to file: \n" +
                    "   file: " + file + "\n" +
                    "   random-access: " + allowRandomAccess);
        }
        return channel;
    }
    catch (Throwable e)
    {
        throw new ContentIOException("Failed to open file channel: " + this, e);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:37,代码来源:FileContentReader.java

示例14: testSimple

import java.nio.channels.Channels; //导入方法依赖的package包/类
@Test
public void testSimple() throws IOException {
    ByteBuffer datagram = createDatagram(5);

    DatagramBuffer datagramBuffer = new DatagramBuffer(9);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    WritableByteChannel channel = Channels.newChannel(bos);

    datagramBuffer.readFrom(datagram);
    datagramBuffer.writeTo(channel);

    byte[] result = bos.toByteArray();
    Assert.assertArrayEquals(datagram.array(), result);
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:15,代码来源:DatagramBufferTest.java

示例15: ioResourceToByteBuffer

import java.nio.channels.Channels; //导入方法依赖的package包/类
/**
 * 
 * @param resource
 * @param bufferSize
 * @return
 * @throws IOException
 */
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = createByteBuffer((int)fc.size() + 1);
            while (fc.read(buffer) != -1) {
                ;
            }
        }
    } else {
        try (
                InputStream source = IOUtil.class.getClassLoader().getResourceAsStream(resource);
                ReadableByteChannel rbc = Channels.newChannel(source)
        ) {
            buffer = createByteBuffer(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
开发者ID:brokenprogrammer,项目名称:Mass,代码行数:41,代码来源:IOUtil.java


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