我在下面用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.
更多推荐
发布评论