2013-09-22 47 views
0

我写了一个ServerResource下面与restlet-android-2.1.4。如果我将SIZE设置为1024 * 12 + 485,它就可以工作。但是,如果我将SIZE更改为1024 * 12 + 486,则此句柄将处于待处理状态。奇怪的失败GET服务器资源与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(); 
      } 
     }; 
    } 
} 
+0

您正在使用哪个Restlet HTTP服务器?您是否将Jetty或Simple扩展添加到类路径中? –

+0

我正在使用默认的Restlet组件来启动服务器。在线程https://gist.github.com/fxp/016f9e39e8f30a3b4edd中运行此代码,无需码头或其他扩展 – fxp

+0

请尝试添加org.restlet.ext.simple.jar和依赖关系,并查看它是否工作得更好。 –

回答

0

潜入代码中,更改WritableSocketChannel.java中的写入函数(在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

某些类型的通道,根据它们的状态,可能只写一些字节或可能根本没有。例如,非阻塞模式下的套接字通道 无法在套接字的输出缓冲区中写入比 空闲的字节更多的字节。

相关问题