/** * The reading loop will continually read until an error occurs or the stream is gracefully ended * by the peer. * * <p>("resource" warning suppression applies to 'bis'. It's not valid because socket itself gets * closed) */ @SuppressWarnings("resource") private void readTextLoop(Socket socket) throws Exception { InputStream in = socket.getInputStream(); BufferedInputStream bis = new BufferedInputStream( new CountableInputStream(in, _counterRecvOps, _counterRecvRate), 1024); // create a buffer that'll be reused // start off small, will grow as needed BufferBuilder bb = new BufferBuilder(256); while (!_shutdown) { int c = bis.read(); if (c < 0) break; if (charMatches((char) c, _receiveDelimiters)) { String str = bb.getTrimmedString(); if (str != null) handleReceivedData(str); bb.reset(); } else { if (bb.getSize() >= MAX_SEGMENT_ALLOWED) { // drop the connection throw new IOException( "Too much data arrived (at least " + bb.getSize() / 1024 + " KB) before any delimeter was present; dropping connection."); } bb.append((byte) c); } } // (while) // the peer has gracefully closed down the connection or we're shutting down if (!_shutdown) { // send out last data String str = bb.getTrimmedString(); if (str != null) handleReceivedData(str); // then fire the disconnected callback Handler.tryHandle(_disconnectedCallback, _callbackErrorHandler); } }