奇怪的失败在ServerResource中获取1024 * 12 + 486长度响应(strange failure GET in ServerResource with 1024*12+486 le

编程入门 行业动态 更新时间:2024-10-28 21:22:35
奇怪的失败在ServerResource中获取1024 * 12 + 486长度响应(strange failure GET in ServerResource with 1024*12+486 length response)

我在下面用restlet-android-2.1.4写了一个ServerResource。 如果我将SIZE设置为1024 * 12 + 485,则可以正常工作。 但是,如果我将SIZE更改为1024 * 12 + 486,则此句柄将处于待处理状态。

public class DataResource extends ServerResource { public static final int SIZE = 1024 * 12 + 485; @Get public Representation getResource(Representation entity) { return new OutputRepresentation(MediaType.ALL) { @Override public void write(OutputStream outputStream) throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < SIZE; i++) { sb.append('E'); } outputStream.write(sb.toString().getBytes()); outputStream.close(); } }; } }

I wrote a ServerResource below with restlet-android-2.1.4. If the I set SIZE to 1024 * 12 + 485, it works. But if I change SIZE to 1024 * 12 + 486 this handle will pending.

public class DataResource extends ServerResource { public static final int SIZE = 1024 * 12 + 485; @Get public Representation getResource(Representation entity) { return new OutputRepresentation(MediaType.ALL) { @Override public void write(OutputStream outputStream) throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < SIZE; i++) { sb.append('E'); } outputStream.write(sb.toString().getBytes()); outputStream.close(); } }; } }

最满意答案

深入研究代码,更改WritableSocketChannel.java中的write函数(在restlet源代码中),它将起作用。 我不知道这是不是一个错误。

public int write(ByteBuffer src) throws IOException { return getWrappedChannel().write(src); }

public int write(ByteBuffer src) throws IOException { int count = 0; while (src.hasRemaining()) { count += getWrappedChannel().write(src); } return count; }

根据java doc 。

某些类型的通道(取决于它们的状态)可能只写入一些字节,或者根本不写。 例如,处于非阻塞模式的套接字通道不能再写入套接字输出缓冲区中可用的字节数。

Dive into the code, change the write function in WritableSocketChannel.java(in restlet source code) and it will work. I don't know whether it's a bug.

from

public int write(ByteBuffer src) throws IOException { return getWrappedChannel().write(src); }

to

public int write(ByteBuffer src) throws IOException { int count = 0; while (src.hasRemaining()) { count += getWrappedChannel().write(src); } return count; }

According to java doc.

Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

更多推荐

本文发布于:2023-08-02 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   奇怪   ServerResource   failure   length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!