天道酬勤,学无止境

nio

SocketChannel.write() 写入问题(SocketChannel.write() writing problem)

问题 这里的问题是我可以看到数据正在写入套接字,但并不总是被发送。 这是一个代码片段 ByteBuffer writeBuffer = ByteBuffer.allocate(8192); writeBuffer.clear(); writeBuffer.put("heartbeat".getBytes()); writeBuffer.flip(); LOG.debug("is connected: " + socketChannel.isConnected()); int bytesWritten = 0; if (key.isWritable()) { while (writeBuffer.hasRemaining()) { bytesWritten += socketChannel.write(writeBuffer); } } 我使用 TCPMon 来查看实际数据是否被写出到套接字 - 确实如此。 但是使用 WireShark(另一个网络监控工具)我看不到数据包通过 NIC。 任何帮助,将不胜感激 回答1 无论如何,您的代码是错误的。 如果写入返回零,则套接字发送缓冲区已满,因此您应该注册 OP_WRITE 并返回到选择循环,而不是浪费时间旋转直到再次有空间。 您目前的技术使其他服务渠道匮乏并浪费 CPU 周期。 此外,此时测试isConnected()是徒劳的。 这是。

2021-12-05 07:56:36    分类:技术分享    java   nio

如何在不使用文件的情况下将 BufferedImage 转换为字节数组(How I can convert BufferedImage to a byte array without using files)

问题 我正在尝试将 BufferedImage 转换为字节数组,但每次出现异常时我都会收到返回 bufferImage 的服务,这是我的代码: BufferedImage bufferedImage = myservice.getImage(); WritableRaster raster = bufferedImage.getRaster(); DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); byte[] fileContent = data.getData(); 此代码引发异常: java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte 如何在不使用文件的情况下进行此转换 回答1 您可以使用ByteArrayOutputStream类并使用以下代码从BufferedImage对象写入数据, BufferedImage image = null; // you have the data in this object ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO

2021-12-05 01:03:06    分类:技术分享    java   nio

线程“main”中的异常 java.nio.file.InvalidPathException: Illegal char <:> at index 2:(Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2:)

问题 我必须将类路径资源从一个包复制到另一个包。 我的程序是: public static void main(String[] args) throws IOException, URISyntaxException { ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader(); InputStream in = classLoader.getResourceAsStream("com/stackoverflow/main/Movie.class"); URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI(); Path path = Paths.get(uri.getPath(),"Movie.class"); System.out.println(path); long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); System.out.println(copy); } 在Files.copy方法我得到异常: Exception in thread "main" java.nio.file

2021-12-01 14:39:31    分类:技术分享    java   nio

Java 8 Path Stream 和 FileSystemException(打开的文件太多)(Java 8 Path Stream and FileSystemException (Too many open files))

问题 天才! 我正在练习 Java 8。 所以如果我做这样的事情: Files.walk(Paths.get(corpusPathStr)) .filter(path -> path.toFile().isFile()) .forEach(path -> { try { Files.lines(path) .forEach(...); } catch (IOException e) { e.printStackTrace(); } }); 我收到 FileSystemException 错误。 如果我在forEach下打开一个文件,会不会打开太多文件? 还是有其他原因导致 FileSystemException(打开的文件太多)? 提前感谢您的帮助! 回答1 用 try(Stream<Path> stream = Files.walk(Paths.get(corpusPathStr))) { stream.filter(path -> Files.isRegularFile(path) && Files.isReadable(path)) .flatMap(path -> { try { return Files.lines(path); } catch (IOException e) { throw new UncheckedIOException(e); } })

2021-12-01 10:57:39    分类:技术分享    java-8   nio

如果有足够的数据,FileChannel.read 读取的字节数是否会少于指定的字节数?(Would FileChannel.read read less bytes than specified if there's enough data?)

问题 比如我有一个文件,它的内容是: abcdefg 然后我使用以下代码读取“defg”。 ByteBuffer bb = ByteBuffer.allocate(4); int read = channel.read(bb, 3); assert(read == 4); 因为文件中有足够的数据所以我可以这么认为吗? 我可以假设只有当文件中没有足够的字节时,该方法才返回小于给定缓冲区限制的数字吗? 回答1 我可以假设只有当文件中没有足够的字节时,该方法才返回小于给定缓冲区限制的数字吗? Javadoc 说: 读取可能不会填满缓冲区 并给出了一些例子,和 返回读取的字节数,可能为零,如果通道已到达流末尾,则返回 -1。 这不足以让您做出这种假设。 在实践中,您很可能在读取文件时总是得到一个完整的缓冲区,以文件结尾为模。 考虑到进行系统调用的开销,从操作系统实现的角度来看,这是有道理的。 但是,我也可以想象返回半空缓冲区可能有意义的情况。 例如,当通过慢速网络链接从本地安装的远程文件系统读取时,返回部分填充的缓冲区有一些优势,以便应用程序可以开始处理数据。 在这种情况下,某些未来的操作系统可能会实现read系统调用来做到这一点。 如果假设您总是会得到一个完整的缓冲区,那么当您的应用程序在(假设的)新平台上运行时,您可能会感到惊讶。 另一个问题是,有一些种类的流

2021-11-30 19:27:23    分类:技术分享    java   nio

如何使用 Gson 序列化 java.nio.file.Path?(How to serialize java.nio.file.Path with Gson?)

问题 尝试序列化包含java.nio.file.Path Object时出现java.lang.StackOverflowError 即使我写道: public class PathConverter implements JsonDeserializer<Path>, JsonSerializer<Path> { @Override public Path deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return Paths.get(jsonElement.getAsString()); } @Override public JsonElement serialize(Path path, Type type, JsonSerializationContext jsonSerializationContext) { return new JsonPrimitive(path.toString()); } } 并应用它: String json = new GsonBuilder() .registerTypeAdapter(Path.class, new

2021-11-30 07:19:22    分类:技术分享    java   gson   nio

How to read efficiently from socket using Java NIO

I am working on task involving reading from the socket trading quotes and I need to achieve minimum latency and high throughput. I started with the simpliest possible java nio prototype like this ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE); try { buf.clear(); int numBytesRead = socketChannel.read(buf); if (numBytesRead == -1) { socketChannel.close(); } else { buf.flip(); byte[] byteArrived = new byte[buf.remaining]; buf.get(byteArrived,0,byteArrived.length); // here we send byteArrived to the parser } } catch (IOException e) { } I guess it is lame to create byte[] array every time

2021-11-29 02:46:59    分类:问答    java   networking   nio

Java:如何使用 nio Path 规范化路径?(Java: how to normalize paths with nio Path?)

问题 java.io.File的真正优点之一是它可以将路径规范化为可预测的格式。 new File("/", inputPath).getPath()总是返回一个相对路径标准化的字符串,并且总是以可预测的路径分隔符开始和结束。 有没有办法用新的 nio Path或Paths类来做到这一点? (另请注意,我正在处理其他系统的抽象路径,这与任何本地文件系统无关) 我想要的更多行为示例: - "/foo" -> "/foo" - "//foo/" -> "/foo" - "foo/" -> "/foo" - "foo/bar" -> "/foo/bar" - "foo/bar/../baz" -> "/foo/baz" - "foo//bar" -> "/foo/bar" 回答1 此代码有效: public final class Foo { private static final List<String> INPUTS = Arrays.asList( "/foo", "//foo", "foo/", "foo/bar", "foo/bar/../baz", "foo//bar" ); public static void main(final String... args) { Path path; for (final String input: INPUTS) { path =

2021-11-29 01:56:06    分类:技术分享    java   nio

Is ThreadLocal safe to use with Tomcat NIO Connector

This just came to mind when testing the Tomcat NIO connector during my load tests. I make use of ThreadLocal's additionally I use Spring, which I know in several places it also makes use of it. Since the NIO connector does not have a thread per connection, I worry that it may result in very hard to find bugs if a ThreadLocal object was shared with another thread before it had been cleaned up. However, I assume that this is not an issue as it is not a documented warning that I could find, nor have I found any other posts warning about this. I assume that the NIO connector has no effect to the

2021-11-28 21:57:01    分类:问答    java   tomcat   nio   thread-local

How to deal with a very large text file?

I'm currently writing something that needs to handle very large text files (a few GiB at least). What's needed here (and this is fixed) is: CSV-based, following RFC 4180 with the exception of embedded line breaks random read access to lines, though mostly line by line and near the end appending lines at the end (changing lines). Obviously that calls for the rest of the file to be rewritten, it's also rare, so not particularly important at the moment The size of the file forbids keeping it completely in memory (which is also not desirable, since when appending the changes should be persisted as

2021-11-28 15:54:17    分类:问答    java   nio   text-files