private void enlargeBuffer() { java.nio.CharBuffer newBuf = java.nio.CharBuffer.allocate(cBuf.capacity() * 2); newBuf.put((char[])cBuf.array(), cBuf.arrayOffset(), cBuf.position()); cBuf = newBuf; //ThreadLocalCache.charBuffer.set(cBuf); }
/// <summary> /// <para> /// Encodes the content of the give character buffer and outputs to a byte /// buffer that is to be returned. /// </para> /// <para>The default action in case of encoding errors is <c>CodingErrorAction.REPLACE</c>.</para> /// </summary> /// <param name="buffer">the character buffer containing the content to be encoded.</param> /// <returns>the result of the encoding.</returns> public java.nio.ByteBuffer encode(java.nio.CharBuffer buffer) { try { return(this.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE).encode( buffer)); } catch (CharacterCodingException ex) { throw new java.lang.Error(ex.getMessage(), ex); } }
/* * original output is full and doesn't have remaining. allocate more space * to new CharBuffer and return it, the contents in the given buffer will be * copied into the new buffer. */ private java.nio.CharBuffer allocateMore(java.nio.CharBuffer output) { if (output.capacity() == 0) { return(java.nio.CharBuffer.allocate(1)); } java.nio.CharBuffer result = java.nio.CharBuffer.allocate(output.capacity() * 2); output.flip(); result.put(output); return(result); }
protected override java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ) { Encoding enc = this.cs.getEncoding(); char[] input = new char[inJ.capacityJ - inJ.positionJ]; inJ.get(input); byte[] output = new byte[input.Length * fiveValue]; int size = enc.GetEncoder().GetBytes(input, 0, input.Length, output, 0, true); outJ.put(output, 0, size); outJ = java.nio.ByteBuffer.wrap((byte[])outJ.array(), 0, size); return(java.nio.charset.CoderResult.UNDERFLOW); }
/** * Reads characters and puts them into the {@code target} character buffer. * * @param target * the destination character buffer. * @return the number of characters put into {@code target} or -1 if the end * of this reader has been reached before a character has been read. * @throws IOException * if any I/O error occurs while reading from this reader. * @throws NullPointerException * if {@code target} is {@code null}. * @throws ReadOnlyBufferException * if {@code target} is read-only. */ public virtual int read(java.nio.CharBuffer target)// throws IOException { if (null == target) { throw new java.lang.NullPointerException(); } int length = target.length(); char[] buf = new char[length]; length = java.lang.Math.min(length, read(buf)); if (length > 0) { target.put(buf, 0, length); } return(length); }
/** * @see * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String) */ public java.nio.ByteBuffer encode(String name) { java.nio.charset.CharsetEncoder enc = this.charset.newEncoder(); enc.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT); enc.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT); java.nio.CharBuffer cb = java.nio.CharBuffer.wrap(name); java.nio.ByteBuffer outJ = java.nio.ByteBuffer.allocate(name.length() + (name.length() + 1) / 2); while (cb.remaining() > 0) { java.nio.charset.CoderResult res = enc.encode(cb, outJ, true); if (res.isUnmappable() || res.isMalformed()) { // write the unmappable characters in utf-16 // pseudo-URL encoding style to ByteBuffer. if (res.length() * 6 > outJ.remaining()) { outJ = ZipEncodingHelper.growBuffer(outJ, outJ.position() + res.length() * 6); } for (int i = 0; i < res.length(); ++i) { ZipEncodingHelper.appendSurrogate(outJ, cb.get()); } } else if (res.isOverflow()) { outJ = ZipEncodingHelper.growBuffer(outJ, 0); } else if (res.isUnderflow()) { enc.flush(outJ); break; } } outJ.limit(outJ.position()); outJ.rewind(); return(outJ); }
/* * This is a facade method for the decoding operation. * <p> * This method decodes the remaining byte sequence of the given byte buffer * into a new character buffer. This method performs a complete decoding * operation, resets at first, then decodes, and flushes at last. * <p> * This method should not be invoked while another {@code decode} operation * is ongoing. * * @param in * the input buffer. * @return a new <code>CharBuffer</code> containing the the characters * produced by this decoding operation. The buffer's limit will be * the position of the last character in the buffer, and the * position will be zero. * @throws IllegalStateException * if another decoding operation is ongoing. * @throws MalformedInputException * if an illegal input byte sequence for this charset was * encountered, and the action for malformed error is * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT} * @throws UnmappableCharacterException * if a legal but unmappable input byte sequence for this * charset was encountered, and the action for unmappable * character error is * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}. * Unmappable means the byte sequence at the input buffer's * current position cannot be mapped to a Unicode character * sequence. * @throws CharacterCodingException * if another exception happened during the decode operation. */ public java.nio.CharBuffer decode(java.nio.ByteBuffer inJ) //throws CharacterCodingException { reset(); int length = (int)(inJ.remaining() * averChars); java.nio.CharBuffer output = java.nio.CharBuffer.allocate(length); CoderResult result = null; while (true) { result = decode(inJ, output, false); checkCoderResult(result); if (result.isUnderflow()) { break; } else if (result.isOverflow()) { output = allocateMore(output); } } result = decode(inJ, output, true); checkCoderResult(result); while (true) { result = flush(output); checkCoderResult(result); if (result.isOverflow()) { output = allocateMore(output); } else { break; } } output.flip(); status = FLUSH; return(output); }
/* * Checks if the given argument is legal as this encoder's replacement byte * array. * * The given byte array is legal if and only if it can be decode into * sixteen bits Unicode characters. * * This method can be overridden for performance improvement. * * @param repl * the given byte array to be checked. * @return true if the the given argument is legal as this encoder's * replacement byte array. */ public bool isLegalReplacement(byte[] repl) { if (decoder == null) { decoder = cs.newDecoder(); } CodingErrorAction malform = decoder.malformedInputAction(); CodingErrorAction unmap = decoder.unmappableCharacterAction(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); java.nio.ByteBuffer inJ = java.nio.ByteBuffer.wrap(repl); java.nio.CharBuffer outJ = java.nio.CharBuffer.allocate((int)(repl.Length * decoder .maxCharsPerByte())); CoderResult result = decoder.decode(inJ, outJ, true); decoder.onMalformedInput(malform); decoder.onUnmappableCharacter(unmap); return(!result.isError()); }
/* * Encodes characters into bytes. This method is called by * {@link #encode(CharBuffer, ByteBuffer, boolean) encode}. * <p /> * This method will implement the essential encoding operation, and it won't * stop encoding until either all the input characters are read, the output * buffer is filled, or some exception is encountered. Then it will * return a <code>CoderResult</code> object indicating the result of the * current encoding operation. The rule to construct the * <code>CoderResult</code> is the same as for * {@link #encode(CharBuffer, ByteBuffer, boolean) encode}. When an * exception is encountered in the encoding operation, most implementations * of this method will return a relevant result object to the * {@link #encode(CharBuffer, ByteBuffer, boolean) encode} method, and some * performance optimized implementation may handle the exception and * implement the error action itself. * <p /> * The buffers are scanned from their current positions, and their positions * will be modified accordingly, while their marks and limits will be * intact. At most {@link CharBuffer#remaining() in.remaining()} characters * will be read, and {@link ByteBuffer#remaining() out.remaining()} bytes * will be written. * <p /> * Note that some implementations may pre-scan the input buffer and return * <code>CoderResult.UNDERFLOW</code> until it receives sufficient input. * <p /> * @param in * the input buffer. * @param out * the output buffer. * @return a <code>CoderResult</code> instance indicating the result. */ protected abstract CoderResult encodeLoop(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ);
/* * Encodes characters starting at the current position of the given input * buffer, and writes the equivalent byte sequence into the given output * buffer from its current position. * <p /> * The buffers' position will be changed with the reading and writing * operation, but their limits and marks will be kept intact. * <p /> * A <code>CoderResult</code> instance will be returned according to * following rules: * <ul> * <li>A {@link CoderResult#malformedForLength(int) malformed input} result * indicates that some malformed input error was encountered, and the * erroneous characters start at the input buffer's position and their * number can be got by result's {@link CoderResult#length() length}. This * kind of result can be returned only if the malformed action is * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.</li> * <li>{@link CoderResult#UNDERFLOW CoderResult.UNDERFLOW} indicates that * as many characters as possible in the input buffer have been encoded. If * there is no further input and no characters left in the input buffer then * this task is complete. If this is not the case then the client should * call this method again supplying some more input characters.</li> * <li>{@link CoderResult#OVERFLOW CoderResult.OVERFLOW} indicates that the * output buffer has been filled, while there are still some characters * remaining in the input buffer. This method should be invoked again with a * non-full output buffer.</li> * <li>A {@link CoderResult#unmappableForLength(int) unmappable character} * result indicates that some unmappable character error was encountered, * and the erroneous characters start at the input buffer's position and * their number can be got by result's {@link CoderResult#length() length}. * This kind of result can be returned only on * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.</li> * </ul> * <p /> * The <code>endOfInput</code> parameter indicates if the invoker can * provider further input. This parameter is true if and only if the * characters in the current input buffer are all inputs for this encoding * operation. Note that it is common and won't cause an error if the invoker * sets false and then has no more input available, while it may cause an * error if the invoker always sets true in several consecutive invocations. * This would make the remaining input to be treated as malformed input. * input. * <p /> * This method invokes the * {@link #encodeLoop(CharBuffer, ByteBuffer) encodeLoop} method to * implement the basic encode logic for a specific charset. * * @param in * the input buffer. * @param out * the output buffer. * @param endOfInput * true if all the input characters have been provided. * @return a <code>CoderResult</code> instance indicating the result. * @throws IllegalStateException * if the encoding operation has already started or no more * input is needed in this encoding process. * @throws CoderMalfunctionError * If the {@link #encodeLoop(CharBuffer, ByteBuffer) encodeLoop} * method threw an <code>BufferUnderflowException</code> or * <code>BufferUnderflowException</code>. */ public CoderResult encode(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ, bool endOfInput) { //If the previous step is encode(CharBuffer), then no more input is needed // thus endOfInput should not be false if (status == READY && finished && !endOfInput) { throw new java.lang.IllegalStateException(); } if ((status == FLUSH) || (!endOfInput && status == END)) { throw new java.lang.IllegalStateException(); } CoderResult result; while (true) { try { result = encodeLoop(inJ, outJ); } catch (BufferOverflowException e) { throw new CoderMalfunctionError(e); } catch (BufferUnderflowException e) { throw new CoderMalfunctionError(e); } if (result == CoderResult.UNDERFLOW) { status = endOfInput ? END : ONGOING; if (endOfInput) { int remaining = inJ.remaining(); if (remaining > 0) { result = CoderResult.malformedForLength(remaining); } else { return(result); } } else { return(result); } } else if (result == CoderResult.OVERFLOW) { status = endOfInput ? END : ONGOING; return(result); } CodingErrorAction action = malformAction; if (result.isUnmappable()) { action = unmapAction; } // If the action is IGNORE or REPLACE, we should continue // encoding. if (action == CodingErrorAction.REPLACE) { if (outJ.remaining() < replace.Length) { return(CoderResult.OVERFLOW); } outJ.put(replace); } else { if (action != CodingErrorAction.IGNORE) { return(result); } } inJ.position(inJ.position() + result.length()); } }
protected override java.nio.charset.CoderResult decodeLoop(java.nio.ByteBuffer inJ, java.nio.CharBuffer outJ) { Encoding enc = this.cs.getEncoding(); byte[] input = new byte[inJ.remaining()]; inJ.get(input); char[] output = new char[input.Length * CharsetEncoderImpl.fiveValue]; int size = enc.GetDecoder().GetChars(input, 0, input.Length, output, 0, true); outJ.put(output, 0, size); return(java.nio.charset.CoderResult.UNDERFLOW); }
/* * Reads at most {@code length} characters from this reader and stores them * at position {@code offset} in the character array {@code buf}. Returns * the number of characters actually read or -1 if the end of the reader has * been reached. The bytes are either obtained from converting bytes in this * reader's buffer or by first filling the buffer from the source * InputStream and then reading from the buffer. * * @param buf * the array to store the characters read. * @param offset * the initial position in {@code buf} to store the characters * read from this reader. * @param length * the maximum number of characters to read. * @return the number of characters read or -1 if the end of the reader has * been reached. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the length of * {@code buf}. * @throws IOException * if this reader is closed or some other I/O error occurs. */ public override int read(char[] buf, int offset, int length) {// throws IOException { lock (lockJ) { if (!isOpen()) { // luni.BA=InputStreamReader is closed. throw new IOException("InputStreamReader is closed."); //$NON-NLS-1$ } if (offset < 0 || offset > buf.Length - length || length < 0) { throw new java.lang.IndexOutOfBoundsException(); } if (length == 0) { return(0); } java.nio.CharBuffer outJ = java.nio.CharBuffer.wrap(buf, offset, length); java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW; // bytes.remaining() indicates number of bytes in buffer // when 1-st time entered, it'll be equal to zero bool needInput = !bytes.hasRemaining(); while (outJ.hasRemaining()) { // fill the buffer if needed if (needInput) { try { if ((inJ.available() == 0) && (outJ.position() > offset)) { // we could return the result without blocking read break; } } catch (IOException e) { // available didn't work so just try the read } int to_read = bytes.capacity() - bytes.limit(); int off = bytes.arrayOffset() + bytes.limit(); int was_red = inJ.read((byte[])bytes.array(), off, to_read); if (was_red == -1) { endOfInput = true; break; } else if (was_red == 0) { break; } bytes.limit(bytes.limit() + was_red); needInput = false; } // decode bytes result = decoder.decode(bytes, outJ, false); if (result.isUnderflow()) { // compact the buffer if no space left if (bytes.limit() == bytes.capacity()) { bytes.compact(); bytes.limit(bytes.position()); bytes.position(0); } needInput = true; } else { break; } } if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput) { result = decoder.decode(bytes, outJ, true); decoder.flush(outJ); decoder.reset(); } if (result.isMalformed()) { throw new java.nio.charset.MalformedInputException(result.length()); } else if (result.isUnmappable()) { throw new java.nio.charset.UnmappableCharacterException(result.length()); } return(outJ.position() - offset == 0 ? -1 : outJ.position() - offset); } }