2013-09-22 45 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

某些類型的通道,根據它們的狀態,可能只寫一些字節或可能根本沒有。例如,非阻塞模式下的套接字通道 無法在套接字的輸出緩衝區中寫入比 空閒的字節更多的字節。

相關問題