Tag: nio

为什么我们从Java 1.6中的FileChannel.map获取ClosedByInterruptException?

我们的客户抱怨,偶尔,我们对FileChannel.map调用因ClosedByInterruptException失败。 Javadoc并未将此列为合法可能性。 有谁知道这里会发生什么? 原因0:java.nio.channels.ClosedByInterruptException Cause0-堆栈跟踪: at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:184) at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:772)

java.util.zip.ZipError:CEN标头无效(签名错误)

我在Red Hat Linux上使用Java 1.7.0_40,我有以下代码: Path zipfile = Paths.get(filename); FileSystem fs = FileSystems.newFileSystem(zipfile, FileTest.class.getClassLoader()); filename变量指向一个788MB的zip文件。 zip文件的未压缩大小为8.3GB。 当我运行上面的代码时,我得到以下exception: Exception in thread “main” java.util.zip.ZipError: invalid CEN header (bad signature) at com.sun.nio.zipfs.ZipFileSystem.zerror(ZipFileSystem.java:1605) at com.sun.nio.zipfs.ZipFileSystem.initCEN(ZipFileSystem.java:1058) at com.sun.nio.zipfs.ZipFileSystem.(ZipFileSystem.java:130) at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:139) at java.nio.file.FileSystems.newFileSystem(FileSystems.java:386) at FileTest.readFromZip(FileTest.java:35) at FileTest.main(FileTest.java:25) 我的印象是Java 7能够处理大型zip文件。 任何人都可以解释为什么会这样吗? 谢谢。

在java.nio中指定连接超时

使用非阻塞I / O,连接到远程地址的代码如下所示: SocketChannel channel = SelectorProvider.provider().openSocketChannel(); channel.configureBlocking(false); channel.connect(address); 然后,当某个选择器显示相应的键isConnectable()时,必须通过在通道上调用finishConnect()来完成连接过程。 有没有办法在使用这个成语时指定连接超时?

Java nio连接正在创建多个套接字级连接,为什么?

我写了一个简单的java nio程序,如下所示 public static void main(String[] args) throws IOException, InterruptedException { InetSocketAddress address = new InetSocketAddress(“127.0.0.1”,1001); Selector incomingMessageSelector = Selector.open(); SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); //到此为止,代码创建了到端口52209和52210的前2个连接 socketChannel.connect(address); socketChannel.register(incomingMessageSelector, SelectionKey.OP_CONNECT); socketChannel.register(incomingMessageSelector, SelectionKey.OP_WRITE); socketChannel.register(incomingMessageSelector, SelectionKey.OP_READ); //然后它创建到端口1001的2个连接 Thread.sleep(900000L); } 我想了解它为什么创建4个连接,使用标准的TCP阻塞库,它往往会创建2个连接。 我使用JDK 1.7和Windows 7。 在图像中,仅有4个突出显示的连接是由客户端创建的。 标有红色的一个连接1条目是服务器端口。 PFA显示这4个连接的图像。 我真的很困惑为什么 Selector incomingMessageSelector = Selector.open(); 在动态端口上创建连接

NIO客户端给出exception:java.net.ConnectException:连接被拒绝:没有进一步的信息

我修改了此处可用于Client和Server My客户端的示例代码: public class Client { public static void main(String[] args) { int n=10000; SocketTest [] st= new SocketTest[n]; for(int i=0;i<n;i++) st[i]= new SocketTest("hi"); for(int i=0;i<n;i++) new Thread(st[i]).start(); } } class SocketTest implements Runnable { private String message = ""; private Selector selector; private int i; public SocketTest(String message){ this.message = message; } @Override public […]

Java NIO服务器

目前我正在研究Java NIO Server(单线程)并遇到了一些问题。 服务器接受传入连接,将初始数据包(数据包包含客户端用于进一步通信的一些数据)写入客户端但不读取它们。 服务器只在我关闭客户端时尝试读取,当然,它返回-1。 在接受连接时,它在以下位置注册: selectionKey = socketChannel.register(_selector, SelectionKey.OP_READ) selectionKey.isReadable()返回false (应该吗?) 在发送初始数据包之前,操作更改为: _selectionKey.interestOps(_selectionKey.interestOps() | SelectionKey.OP_WRITE) 发送初始数据包后,操作更改为: selectedKey.interestOps(selectedKey.interestOps() & ~SelectionKey.OP_WRITE) 数据包被发送。 可能是什么问题呢? 它可以与客户有关吗?

扩展ByteBuffer类

有没有办法创建扩展ByteBuffer类的类? ByteBuffer的一些抽象方法是包私有的,如果我创建包java.nio,则抛出安全exception。 出于性能原因,我想这样做 – 例如,getInt有大约10个方法调用,以及相当多的if。 即使剩下所有检查,并且只有方法调用被内联并且大/小端检查被删除,我创建的测试表明它可以快4倍。

使用Java NIO进行10000并发连接

我使用Java nio编写了一个服务器(类似于一个)和客户端代码 。 我正在努力实现尽可能多的连接。 从之前的建议开始,我放慢了客户端创建的过程,为操作系统(Windows 8)提供了足够的时间来处理请求。 我在不同的机器上运行客户端代码,以便Server具有所有可用的运行空间。 当我尝试创建10,000个连接时,大约8500个连接并且rest被拒绝连接,并且拒绝连接客户端(客户端代码中的线程)发生的更多(稍后在客户端代码中进行循环)。 我的CPU和内存使用率非常高。我看到大多数(占总CPU消耗的48%)被select方法消耗(主要由gui事件rest)。 是因为这么多客户? 我也看到有些人在JRE7中抱怨这个错误,并建议使用JRE6。 对于javaw.exe进程,内存使用率为2000+ MB。(我注意到1个进程使用了​​低内存但主要CPU使用率。)当所有8500个左右的客户端连接时,总体使用率约为98%。 系统也多次绞死但继续服务。我看到非页面池内存使用量在这个过程中从178 MB增加到310 MB(最大限制是多少?)。是不是因为当我们写入套接字时非页面汇集内存使用? 任何人都可以告诉我可能会遇到哪些限制因此10,000次成功连接是不可能的? (每个进程限制的套接字?)(非分页内存?)(Backlog Queue再次?)调整可能允许限制被推送? (Windows机器) 我在4GB系统上使用Windows 8。 ` public class Server implements Runnable { public final static String ADDRESS = “192.168.2.14”; public final static int PORT = 8511; public final static long TIMEOUT = 10000; public int clients; ByteBuffer readBuffer […]

带有multithreading的Java NIO SocketChannel.read()

我正在使用Java NIO实现一个简单的文件服务器,其中包含一个选择线程和多个工作线程(用于执行实际读/写)。 代码的主要部分如下所示: while (true) { int num = selector.select(); if (num > 0) { Iterator keys = selector.selectedKeys().iterator(); final SelectionKey key = keys.next(); keys.remove(); if (key.isValid()) { if (key.isAcceptable()) { accept(key); } else if (key.isReadable()) { performReadInWorkerThread (key); } else if (key.isWritable()) { performWriteInWorkerThread (key); } } } } 从代码片段中可以看出,当选择可读/可写通道时,我将读/写从选择线程卸载到工作线程。 现在的问题是,当一个可读通道被移交给工作线程时,在它完成/开始从通道读取之前,选择线程再次循环,并且selector.select()选择先前选择的可读通道(因为仍然存在通道中的输入缓冲区尚未被先前分配的工作线程完全占用,因此通道再次移交给另一个工作线程,导致多个工作线程读取同一通道 。 我相信这是一个设计问题。 我的问题是如何确保只有一个线程同时读取一个频道?

使用ZipFileSystem压缩一个巨大的文件夹会导致OutOfMemoryError

通过将zip文件视为文件系统, java.nio包有一种处理zip文件的漂亮方式。 这使我们能够像普通文件一样处理zip文件内容。 因此,只需使用Files.copy将所有文件复制到zip文件中即可实现压缩整个文件夹。 由于子文件夹也要复制,我们需要一个访问者: private static class CopyFileVisitor extends SimpleFileVisitor { private final Path targetPath; private Path sourcePath = null; public CopyFileVisitor(Path targetPath) { this.targetPath = targetPath; } @Override public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { if (sourcePath == null) { sourcePath = dir; } else { Files.createDirectories(targetPath.resolve(sourcePath .relativize(dir).toString())); } […]