0

我用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();
            }
        };
    }
}
4

1 回答 1

0

深入代码,更改 WritableSocketChannel.java 中的 write 函数(在 restlet 源代码中),它将起作用。不知道是不是bug。

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 文档

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

于 2013-09-23T07:26:50.260 回答