| 17 | public class Util { |
| 18 | public static boolean SUPRESS_DEBUG_EXCEPTIONS = false; |
| 19 | public static void emitAllData(DataEmitter emitter, ByteBufferList list) { |
| 20 | int remaining; |
| 21 | DataCallback handler = null; |
| 22 | while (!emitter.isPaused() && (handler = emitter.getDataCallback()) != null && (remaining = list.remaining()) > 0) { |
| 23 | handler.onDataAvailable(emitter, list); |
| 24 | if (remaining == list.remaining() && handler == emitter.getDataCallback() && !emitter.isPaused()) { |
| 25 | // this is generally indicative of failure... |
| 26 | |
| 27 | // 1) The data callback has not changed |
| 28 | // 2) no data was consumed |
| 29 | // 3) the data emitter was not paused |
| 30 | |
| 31 | // call byteBufferList.recycle() or read all the data to prevent this assertion. |
| 32 | // this is nice to have, as it identifies protocol or parsing errors. |
| 33 | |
| 34 | // System.out.println("Data: " + list.peekString()); |
| 35 | System.out.println("handler: " + handler); |
| 36 | list.recycle(); |
| 37 | if (SUPRESS_DEBUG_EXCEPTIONS) |
| 38 | return; |
| 39 | throw new RuntimeException("mDataHandler failed to consume data, yet remains the mDataHandler."); |
| 40 | } |
| 41 | } |
| 42 | if (list.remaining() != 0 && !emitter.isPaused()) { |
| 43 | // not all the data was consumed... |
| 44 | // call byteBufferList.recycle() or read all the data to prevent this assertion. |
| 45 | // this is nice to have, as it identifies protocol or parsing errors. |
| 46 | // System.out.println("Data: " + list.peekString()); |
| 47 | System.out.println("handler: " + handler); |
| 48 | System.out.println("emitter: " + emitter); |
| 49 | list.recycle(); |
| 50 | if (SUPRESS_DEBUG_EXCEPTIONS) |
| 51 | return; |
| 52 | // throw new AssertionError("Not all data was consumed by Util.emitAllData"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static void pump(final InputStream is, final DataSink ds, final CompletedCallback callback) { |
| 57 | pump(is, Integer.MAX_VALUE, ds, callback); |