@Override public long skip(long n) throws IOException { if (closed) { throw new IOException(sm.getString("inputBuffer.streamClosed")); } if (n < 0) { throw new IllegalArgumentException(); } long nRead = 0; while (nRead < n) { if (cb.getLength() >= n) { cb.setOffset(cb.getStart() + (int) n); nRead = n; } else { nRead += cb.getLength(); cb.setOffset(cb.getEnd()); int toRead = 0; if (cb.getChars().length < (n - nRead)) { toRead = cb.getChars().length; } else { toRead = (int) (n - nRead); } int nb = realReadChars(cb.getChars(), 0, toRead); if (nb < 0) { break; } } } return nRead; }
@Override public void mark(int readAheadLimit) throws IOException { if (closed) { throw new IOException(sm.getString("inputBuffer.streamClosed")); } if (cb.getLength() <= 0) { cb.setOffset(0); cb.setEnd(0); } else { if ((cb.getBuffer().length > (2 * size)) && (cb.getLength()) < (cb.getStart())) { System.arraycopy(cb.getBuffer(), cb.getStart(), cb.getBuffer(), 0, cb.getLength()); cb.setEnd(cb.getLength()); cb.setOffset(0); } } cb.setLimit(cb.getStart() + readAheadLimit + size); markPos = cb.getStart(); }
@Override public int realReadChars(char cbuf[], int off, int len) throws IOException { if (!gotEnc) { setConverter(); } boolean eof = false; if (bb.getLength() <= 0) { int nRead = realReadBytes(bb.getBytes(), 0, bb.getBytes().length); if (nRead < 0) { eof = true; } } if (markPos == -1) { cb.setOffset(0); cb.setEnd(0); } else { // Make sure there's enough space in the worst case cb.makeSpace(bb.getLength()); if ((cb.getBuffer().length - cb.getEnd()) == 0) { // We went over the limit cb.setOffset(0); cb.setEnd(0); markPos = -1; } } state = CHAR_STATE; conv.convert(bb, cb, eof); if (cb.getLength() == 0 && eof) { return -1; } else { return cb.getLength(); } }
public int available() { int available = 0; if (state == BYTE_STATE) { available = bb.getLength(); } else if (state == CHAR_STATE) { available = cb.getLength(); } if (available == 0) { coyoteRequest.action(ActionCode.AVAILABLE, null); available = (coyoteRequest.getAvailable() > 0) ? 1 : 0; } return available; }
/** Character conversion of the URI. */ protected void convertURI(MessageBytes uri, Request request) throws Exception { ByteChunk bc = uri.getByteChunk(); int length = bc.getLength(); CharChunk cc = uri.getCharChunk(); cc.allocate(length, -1); String enc = connector.getURIEncoding(); if (enc != null) { B2CConverter conv = request.getURIConverter(); try { if (conv == null) { conv = new B2CConverter(enc, true); request.setURIConverter(conv); } else { conv.recycle(); } } catch (IOException e) { log.error("Invalid URI encoding; using HTTP default"); connector.setURIEncoding(null); } if (conv != null) { try { conv.convert(bc, cc, true); uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength()); return; } catch (IOException ioe) { // Should never happen as B2CConverter should replace // problematic characters request.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST); } } } // Default encoding: fast conversion for ISO-8859-1 byte[] bbuf = bc.getBuffer(); char[] cbuf = cc.getBuffer(); int start = bc.getStart(); for (int i = 0; i < length; i++) { cbuf[i] = (char) (bbuf[i + start] & 0xff); } uri.setChars(cbuf, 0, length); }