/**
  * Wraps the result of {@linkplain #getCharContent} in a Reader. Subclasses can change this
  * behavior as long as the contract of {@link FileObject} is obeyed.
  *
  * @param ignoreEncodingErrors {@inheritDoc}
  * @return a Reader wrapping the result of getCharContent
  * @throws IllegalStateException {@inheritDoc}
  * @throws UnsupportedOperationException {@inheritDoc}
  * @throws IOException {@inheritDoc}
  */
 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   CharSequence charContent = getCharContent(ignoreEncodingErrors);
   if (charContent == null) throw new UnsupportedOperationException();
   if (charContent instanceof CharBuffer) {
     CharBuffer buffer = (CharBuffer) charContent;
     if (buffer.hasArray()) return new CharArrayReader(buffer.array());
   }
   return new StringReader(charContent.toString());
 }
Exemple #2
0
 private BigDecimal toBigDecimal() {
   if (lineBuffer.hasArray()) {
     char[] array = lineBuffer.array();
     int offset = lineBuffer.arrayOffset() + lineBuffer.position();
     int length = lineBuffer.remaining();
     return new BigDecimal(array, offset, length);
   } else {
     return new BigDecimal(lineBuffer.toString());
   }
 }
 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;
 }
Exemple #4
0
  /**
   * Creates a new big-endian buffer whose content is a subregion of the specified {@code string}
   * encoded in the specified {@code charset}. The new buffer's {@code readerIndex} and {@code
   * writerIndex} are {@code 0} and the length of the encoded string respectively.
   */
  public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
      throw new NullPointerException("string");
    }
    if (length == 0) {
      return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
      CharBuffer buf = (CharBuffer) string;
      if (buf.hasArray()) {
        return copiedBuffer(
            buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
      }

      buf = buf.slice();
      buf.limit(length);
      buf.position(offset);
      return copiedBuffer(buf, charset);
    }

    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
  }
 protected final CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
   if (src.hasArray() && dst.hasArray()) return encodeArrayLoop(src, dst);
   else return encodeBufferLoop(src, dst);
 }
 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
   if (src.hasArray() && dst.hasArray()) return decodeArrayLoop(src, dst);
   else return decodeBufferLoop(src, dst);
 }
 protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb) {
   int cbRemaining = cb.remaining();
   if (CharsetProviderImpl.hasLoadedNatives()
       && bb.isDirect()
       && bb.hasRemaining()
       && cb.hasArray()) {
     int toProceed = bb.remaining();
     int cbPos = cb.position();
     int bbPos = bb.position();
     boolean throwOverflow = false;
     if (cbRemaining < toProceed) {
       toProceed = cbRemaining;
       throwOverflow = true;
     }
     int res =
         nDecode(
             cb.array(),
             cb.arrayOffset() + cbPos,
             toProceed,
             AddressUtil.getDirectBufferAddress(bb),
             bbPos);
     bb.position(bbPos + res);
     cb.position(cbPos + res);
     if (throwOverflow) return CoderResult.OVERFLOW;
   } else {
     if (bb.hasArray() && cb.hasArray()) {
       int rem = bb.remaining();
       rem = cbRemaining >= rem ? rem : cbRemaining;
       byte[] bArr = bb.array();
       char[] cArr = cb.array();
       int bStart = bb.position();
       int cStart = cb.position();
       int i;
       for (i = bStart; i < bStart + rem; i++) {
         char in = (char) (bArr[i] & 0xFF);
         if (in >= 4) {
           int index = (int) in - 4;
           cArr[cStart++] = (char) arr[index];
         } else {
           cArr[cStart++] = (char) (in & 0xFF);
         }
       }
       bb.position(i);
       cb.position(cStart);
       if (rem == cbRemaining && bb.hasRemaining()) return CoderResult.OVERFLOW;
     } else {
       while (bb.hasRemaining()) {
         if (cbRemaining == 0) return CoderResult.OVERFLOW;
         char in = (char) (bb.get() & 0xFF);
         if (in >= 4) {
           int index = (int) in - 4;
           cb.put(arr[index]);
         } else {
           cb.put((char) (in & 0xFF));
         }
         cbRemaining--;
       }
     }
   }
   return CoderResult.UNDERFLOW;
 }
 protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
   int bbRemaining = bb.remaining();
   if (CharsetProviderImpl.hasLoadedNatives()
       && bb.isDirect()
       && cb.hasRemaining()
       && cb.hasArray()) {
     int toProceed = cb.remaining();
     int cbPos = cb.position();
     int bbPos = bb.position();
     boolean throwOverflow = false;
     if (bbRemaining < toProceed) {
       toProceed = bbRemaining;
       throwOverflow = true;
     }
     int[] res = {toProceed, 0};
     nEncode(
         AddressUtil.getDirectBufferAddress(bb),
         bbPos,
         cb.array(),
         cb.arrayOffset() + cbPos,
         res);
     if (res[0] <= 0) {
       bb.position(bbPos - res[0]);
       cb.position(cbPos - res[0]);
       if (res[1] != 0) {
         if (res[1] < 0) return CoderResult.malformedForLength(-res[1]);
         else return CoderResult.unmappableForLength(res[1]);
       }
     } else {
       bb.position(bbPos + res[0]);
       cb.position(cbPos + res[0]);
       if (throwOverflow) return CoderResult.OVERFLOW;
     }
   } else {
     if (bb.hasArray() && cb.hasArray()) {
       byte[] byteArr = bb.array();
       char[] charArr = cb.array();
       int rem = cb.remaining();
       int byteArrStart = bb.position();
       rem = bbRemaining <= rem ? bbRemaining : rem;
       int x;
       for (x = cb.position(); x < cb.position() + rem; x++) {
         char c = charArr[x];
         if (c > (char) 0x20AC) {
           if (c >= 0xD800 && c <= 0xDFFF) {
             if (x + 1 < cb.limit()) {
               char c1 = charArr[x + 1];
               if (c1 >= 0xD800 && c1 <= 0xDFFF) {
                 cb.position(x);
                 bb.position(byteArrStart);
                 return CoderResult.unmappableForLength(2);
               }
             } else {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.UNDERFLOW;
             }
             cb.position(x);
             bb.position(byteArrStart);
             return CoderResult.malformedForLength(1);
           }
           cb.position(x);
           bb.position(byteArrStart);
           return CoderResult.unmappableForLength(1);
         } else {
           if (c < 0x04) {
             byteArr[byteArrStart++] = (byte) c;
           } else {
             int index = (int) c >> 8;
             index = encodeIndex[index];
             if (index < 0) {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.unmappableForLength(1);
             }
             index <<= 8;
             index += (int) c & 0xFF;
             if ((byte) arr[index] != 0) {
               byteArr[byteArrStart++] = (byte) arr[index];
             } else {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.unmappableForLength(1);
             }
           }
         }
       }
       cb.position(x);
       bb.position(byteArrStart);
       if (rem == bbRemaining && cb.hasRemaining()) {
         return CoderResult.OVERFLOW;
       }
     } else {
       while (cb.hasRemaining()) {
         if (bbRemaining == 0) return CoderResult.OVERFLOW;
         char c = cb.get();
         if (c > (char) 0x20AC) {
           if (c >= 0xD800 && c <= 0xDFFF) {
             if (cb.hasRemaining()) {
               char c1 = cb.get();
               if (c1 >= 0xD800 && c1 <= 0xDFFF) {
                 cb.position(cb.position() - 2);
                 return CoderResult.unmappableForLength(2);
               } else {
                 cb.position(cb.position() - 1);
               }
             } else {
               cb.position(cb.position() - 1);
               return CoderResult.UNDERFLOW;
             }
             cb.position(cb.position() - 1);
             return CoderResult.malformedForLength(1);
           }
           cb.position(cb.position() - 1);
           return CoderResult.unmappableForLength(1);
         } else {
           if (c < 0x04) {
             bb.put((byte) c);
           } else {
             int index = (int) c >> 8;
             index = encodeIndex[index];
             if (index < 0) {
               cb.position(cb.position() - 1);
               return CoderResult.unmappableForLength(1);
             }
             index <<= 8;
             index += (int) c & 0xFF;
             if ((byte) arr[index] != 0) {
               bb.put((byte) arr[index]);
             } else {
               cb.position(cb.position() - 1);
               return CoderResult.unmappableForLength(1);
             }
           }
           bbRemaining--;
         }
       }
     }
   }
   return CoderResult.UNDERFLOW;
 }