本文整理汇总了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;
}
}
示例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;
}
}
示例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();
}
}
示例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());
}
示例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();
}
示例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);
}
}
示例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();
}
示例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());
}
示例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();
}
示例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();
}
示例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());
}
示例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);
}
示例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);
}
}
示例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);
}
示例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;
}