Пример #1
0
 protected int allocateSpace() {
   int spaceLeft = currentWriteChunk.spaceLeft();
   if (spaceLeft == 0) {
     chunks.add(currentWriteChunk);
     totalBytesUnreadInList += currentWriteChunk.bytesUnread();
     currentWriteChunk = new StreamByteBufferChunk(chunkSize);
     spaceLeft = currentWriteChunk.spaceLeft();
   }
   return spaceLeft;
 }
Пример #2
0
 public void reset() {
   if (readMode == ReadMode.RETAIN_AFTER_READING) {
     readIterator = null;
     prepareRetainAfterReading();
     if (currentWriteChunk != null) {
       currentWriteChunk.reset();
     }
   }
 }
Пример #3
0
 public int totalBytesUnread() {
   int total = 0;
   if (readMode == ReadMode.REMOVE_AFTER_READING) {
     total = totalBytesUnreadInList;
   } else if (readMode == ReadMode.RETAIN_AFTER_READING) {
     prepareRetainAfterReading();
     total = totalBytesUnreadInIterator;
   }
   if (currentReadChunk != null) {
     total += currentReadChunk.bytesUnread();
   }
   if (currentWriteChunk != currentReadChunk && currentWriteChunk != null) {
     if (readMode == ReadMode.REMOVE_AFTER_READING) {
       total += currentWriteChunk.bytesUnread();
     } else if (readMode == ReadMode.RETAIN_AFTER_READING) {
       total += currentWriteChunk.bytesUsed();
     }
   }
   return total;
 }
Пример #4
0
 protected int prepareRead() {
   prepareRetainAfterReading();
   int bytesUnread = (currentReadChunk != null) ? currentReadChunk.bytesUnread() : 0;
   if (bytesUnread == 0) {
     if (readMode == ReadMode.REMOVE_AFTER_READING && !chunks.isEmpty()) {
       currentReadChunk = chunks.removeFirst();
       bytesUnread = currentReadChunk.bytesUnread();
       totalBytesUnreadInList -= bytesUnread;
     } else if (readMode == ReadMode.RETAIN_AFTER_READING && readIterator.hasNext()) {
       currentReadChunk = readIterator.next();
       currentReadChunk.reset();
       bytesUnread = currentReadChunk.bytesUnread();
       totalBytesUnreadInIterator -= bytesUnread;
     } else if (currentReadChunk != currentWriteChunk) {
       currentReadChunk = currentWriteChunk;
       bytesUnread = currentReadChunk.bytesUnread();
     } else {
       bytesUnread = -1;
     }
   }
   return bytesUnread;
 }
Пример #5
0
 public String readAsString(Charset charset) throws CharacterCodingException {
   int unreadSize = totalBytesUnread();
   if (unreadSize > 0) {
     CharsetDecoder decoder =
         charset
             .newDecoder()
             .onMalformedInput(CodingErrorAction.REPLACE)
             .onUnmappableCharacter(CodingErrorAction.REPLACE);
     CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
     ByteBuffer buf = null;
     while (prepareRead() != -1) {
       buf = currentReadChunk.readToNioBuffer();
       boolean endOfInput = (prepareRead() == -1);
       CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
       if (endOfInput) {
         if (!result.isUnderflow()) {
           result.throwException();
         }
       }
     }
     CoderResult result = decoder.flush(charbuffer);
     if (buf.hasRemaining()) {
       throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
     }
     if (!result.isUnderflow()) result.throwException();
     charbuffer.flip();
     String str;
     if (charbuffer.hasArray()) {
       int len = charbuffer.remaining();
       char[] ch = charbuffer.array();
       if (len != ch.length) {
         ch = ArrayUtils.subarray(ch, 0, len);
       }
       str = StringCharArrayAccessor.createString(ch);
     } else {
       str = charbuffer.toString();
     }
     return str;
   }
   return null;
 }
Пример #6
0
 public void writeTo(OutputStream target) throws IOException {
   while (prepareRead() != -1) {
     currentReadChunk.writeTo(target);
   }
 }