// implementation of canEncode private bool implCanEncode(java.nio.CharBuffer cb) { if (status == FLUSH || status == INIT) { status = READY; } if (status != READY) { throw new System.InvalidOperationException("encoding already in progress"); } java.nio.charset.CodingErrorAction malformBak = _malformedInputAction; java.nio.charset.CodingErrorAction unmapBak = _unmappableCharacterAction; onMalformedInput(java.nio.charset.CodingErrorAction.REPORT); onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT); bool result = true; try { this.encode(cb); } catch (java.nio.charset.CharacterCodingException) { result = false; } onMalformedInput(malformBak); onUnmappableCharacter(unmapBak); reset(); return(result); }
public override java.lang.CharSequence SubSequence(int start, int end) { checkStartEndRemaining(start, end); java.nio.CharBuffer result = duplicate(); result.limit(_position + end); result.position(_position + start); return(result); }
public override void write(char[] buffer, int offset, int count) { lock (@lock) { checkStatus(); java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, count); java.nio.CharBuffer chars = java.nio.CharBuffer.wrap(buffer, offset, count); convert(chars); } }
public override void write(int oneChar) { lock (@lock) { checkStatus(); java.nio.CharBuffer chars = java.nio.CharBuffer.wrap(new char[] { (char)oneChar } ); convert(chars); } }
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 global::java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer arg0, java.nio.ByteBuffer arg1) { global::MonoJavaBridge.JNIEnv @__env = global::MonoJavaBridge.JNIEnv.ThreadEnv; if (!IsClrObject) { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetEncoder_._encodeLoop14745, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg1))) as java.nio.charset.CoderResult); } else { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallNonVirtualObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetEncoder_.staticClass, global::java.nio.charset.CharsetEncoder_._encodeLoop14745, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg1))) as java.nio.charset.CoderResult); } }
public virtual global::java.nio.charset.CoderResult encode(java.nio.CharBuffer arg0, java.nio.ByteBuffer arg1, bool arg2) { global::MonoJavaBridge.JNIEnv @__env = global::MonoJavaBridge.JNIEnv.ThreadEnv; if (!IsClrObject) { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetEncoder._encode14722, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg1), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg2))) as java.nio.charset.CoderResult); } else { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallNonVirtualObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetEncoder.staticClass, global::java.nio.charset.CharsetEncoder._encode14722, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg1), global::MonoJavaBridge.JavaBridge.ConvertToValue(arg2))) as java.nio.charset.CoderResult); } }
public virtual int read(java.nio.CharBuffer target) { int length = target.Length; char[] buf = new char[length]; length = System.Math.Min(length, read(buf)); if (length > 0) { target.put(buf, 0, length); } return(length); }
public virtual global::java.nio.charset.CoderResult flush(java.nio.CharBuffer arg0) { global::MonoJavaBridge.JNIEnv @__env = global::MonoJavaBridge.JNIEnv.ThreadEnv; if (!IsClrObject) { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetDecoder._flush14700, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))) as java.nio.charset.CoderResult); } else { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallNonVirtualObjectMethod(this.JvmHandle, global::java.nio.charset.CharsetDecoder.staticClass, global::java.nio.charset.CharsetDecoder._flush14700, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))) as java.nio.charset.CoderResult); } }
public virtual int read(java.nio.CharBuffer arg0) { global::MonoJavaBridge.JNIEnv @__env = global::MonoJavaBridge.JNIEnv.ThreadEnv; if (!IsClrObject) { return(@__env.CallIntMethod(this.JvmHandle, global::java.nio.CharBuffer._read14132, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))); } else { return(@__env.CallNonVirtualIntMethod(this.JvmHandle, global::java.nio.CharBuffer.staticClass, global::java.nio.CharBuffer._read14132, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))); } }
/// <summary> /// Returns a new /// <code>ByteBuffer</code> /// containing the bytes encoding the characters from /// <code>buffer</code> /// . /// This method uses /// <code>CodingErrorAction.REPLACE</code> /// . /// <p>Applications should generally create a /// <see cref="CharsetEncoder">CharsetEncoder</see> /// using /// <see cref="newEncoder()">newEncoder()</see> /// for performance. /// </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(newEncoder().onMalformedInput(java.nio.charset.CodingErrorAction.REPLACE). onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPLACE).encode(buffer)); } catch (java.nio.charset.CharacterCodingException ex) { throw new System.Exception(ex.Message, ex); } }
public virtual global::java.nio.CharBuffer put(java.nio.CharBuffer arg0) { global::MonoJavaBridge.JNIEnv @__env = global::MonoJavaBridge.JNIEnv.ThreadEnv; if (!IsClrObject) { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallObjectMethod(this.JvmHandle, global::java.nio.CharBuffer._put14112, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))) as java.nio.CharBuffer); } else { return(global::MonoJavaBridge.JavaBridge.WrapJavaObject(@__env.CallNonVirtualObjectMethod(this.JvmHandle, global::java.nio.CharBuffer.staticClass, global::java.nio.CharBuffer._put14112, global::MonoJavaBridge.JavaBridge.ConvertToValue(arg0))) as java.nio.CharBuffer); } }
/// <summary>Flushes this decoder.</summary> /// <remarks> /// Flushes this decoder. /// This method will call /// <see cref="implFlush(java.nio.CharBuffer)">implFlush</see> /// . Some /// decoders may need to write some characters to the output buffer when they /// have read all input bytes; subclasses can override /// <see cref="implFlush(java.nio.CharBuffer)">implFlush</see> /// to perform the writing operation. /// <p> /// The maximum number of written bytes won't be larger than /// <see cref="java.nio.Buffer.remaining()">out.remaining()</see> /// . If some decoder wants to /// write more bytes than an output buffer's remaining space allows, then a /// <code>CoderResult.OVERFLOW</code> will be returned, and this method /// must be called again with a character buffer that has more remaining /// space. Otherwise this method will return /// <code>CoderResult.UNDERFLOW</code>, which means one decoding process /// has been completed successfully. /// <p> /// During the flush, the output buffer's position will be changed /// accordingly, while its mark and limit will be intact. /// </remarks> /// <param name="out">the given output buffer.</param> /// <returns> /// <code>CoderResult.UNDERFLOW</code> or /// <code>CoderResult.OVERFLOW</code>. /// </returns> /// <exception cref="System.InvalidOperationException"> /// if this decoder hasn't read all input bytes during one /// decoding process, which means neither after calling /// <see cref="decode(java.nio.ByteBuffer)">decode(ByteBuffer)</see> /// nor after /// calling /// <see cref="decode(java.nio.ByteBuffer, java.nio.CharBuffer, bool)">decode(ByteBuffer, CharBuffer, boolean) /// </see> /// with true as value /// for the last boolean parameter. /// </exception> public java.nio.charset.CoderResult flush(java.nio.CharBuffer @out) { if (status != END && status != INIT) { throw new System.InvalidOperationException(); } java.nio.charset.CoderResult result = implFlush(@out); if (result == java.nio.charset.CoderResult.UNDERFLOW) { status = FLUSH; } return(result); }
/// <summary> /// Attempts to read characters into the specified character buffer. /// The buffer is used as a repository of characters as-is: the only /// changes made are the results of a put operation. No flipping or /// rewinding of the buffer is performed. /// </summary> /// <param name="target"> the buffer to read characters into </param> /// <returns> The number of characters added to the buffer, or /// -1 if this source of characters is at its end </returns> /// <exception cref="IOException"> if an I/O error occurs </exception> /// <exception cref="NullPointerException"> if target is null </exception> /// <exception cref="java.nio.ReadOnlyBufferException"> if target is a read only buffer /// @since 1.5 </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public int read(java.nio.CharBuffer target) throws IOException public virtual int Read(java.nio.CharBuffer target) { int len = target.Remaining(); char[] cbuf = new char[len]; int n = Read(cbuf, 0, len); if (n > 0) { target.Put(cbuf, 0, n); } return(n); }
// default implementation is empty /// <summary> /// Checks if the given argument is legal as this encoder's replacement byte /// array. /// </summary> /// <remarks> /// 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. /// </remarks> /// <param name="replacement">the given byte array to be checked.</param> /// <returns> /// true if the the given argument is legal as this encoder's /// replacement byte array. /// </returns> public virtual bool isLegalReplacement(byte[] replacement_1) { if (decoder == null) { decoder = cs.newDecoder(); decoder.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT); decoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT); } java.nio.ByteBuffer @in = java.nio.ByteBuffer.wrap(replacement_1); java.nio.CharBuffer @out = java.nio.CharBuffer.allocate((int)(replacement_1.Length * decoder.maxCharsPerByte())); java.nio.charset.CoderResult result = decoder.decode(@in, @out, true); return(!result.isError()); }
/// <summary> /// Copys the elements of a char buffer to a byte buffer /// Concurency: access must be externally synchronized /// </summary> /// <param name="src"/> /// <param name="dest"/> /// <returns>dest for easy invocation chaining</returns> public static java.nio.ByteBuffer copy(java.nio.CharBuffer src, java.nio.ByteBuffer dest) { int length = src.Length; int i; for (i = 0; i < length; i++) { dest.put(i, unchecked ((byte)src.get(i))); } for (; i < dest.limit(); i++) { dest.put(i, unchecked ((byte)0)); } return(dest); }
/// <summary>This is a facade method for the decoding operation.</summary> /// <remarks> /// 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</code> /// operation /// is ongoing. /// </remarks> /// <param name="in">the input buffer.</param> /// <returns> /// 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. /// </returns> /// <exception cref="System.InvalidOperationException">if another decoding operation is ongoing. /// </exception> /// <exception cref="MalformedInputException"> /// if an illegal input byte sequence for this charset was /// encountered, and the action for malformed error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// </exception> /// <exception cref="UnmappableCharacterException"> /// if a legal but unmappable input byte sequence for this /// charset was encountered, and the action for unmappable /// character error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// . /// Unmappable means the byte sequence at the input buffer's /// current position cannot be mapped to a Unicode character /// sequence. /// </exception> /// <exception cref="CharacterCodingException">if another exception happened during the decode operation. /// </exception> /// <exception cref="java.nio.charset.CharacterCodingException"></exception> public java.nio.CharBuffer decode(java.nio.ByteBuffer @in) { reset(); int length = (int)(@in.remaining() * _averageCharsPerByte); java.nio.CharBuffer output = java.nio.CharBuffer.allocate(length); java.nio.charset.CoderResult result = null; while (true) { result = decode(@in, output, false); checkCoderResult(result); if (result.isUnderflow()) { break; } else { if (result.isOverflow()) { output = allocateMore(output); } } } result = decode(@in, 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); }
/// <exception cref="System.IO.IOException"></exception> private void drainEncoder() { // Strictly speaking, I think it's part of the CharsetEncoder contract that you call // encode with endOfInput true before flushing. Our ICU-based implementations don't // actually need this, and you'd hope that any reasonable implementation wouldn't either. // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway! java.nio.CharBuffer chars = java.nio.CharBuffer.allocate(0); while (true) { java.nio.charset.CoderResult result = encoder.encode(chars, bytes, true); if (result.isError()) { result.throwException(); } else { if (result.isOverflow()) { flushBytes(false); continue; } } break; } // Some encoders (such as ISO-2022-JP) have stuff to write out after all the // characters (such as shifting back into a default state). In our implementation, // this is actually the first time ICU is told that we've run out of input. java.nio.charset.CoderResult result_1 = encoder.flush(bytes); while (!result_1.isUnderflow()) { if (result_1.isOverflow()) { flushBytes(false); result_1 = encoder.flush(bytes); } else { result_1.throwException(); } } }
/// <exception cref="System.IO.IOException"></exception> private void convert(java.nio.CharBuffer chars) { while (true) { java.nio.charset.CoderResult result = encoder.encode(chars, bytes, false); if (result.isOverflow()) { // Make room and try again. flushBytes(false); continue; } else { if (result.isError()) { result.throwException(); } } break; } }
public override void write(string str, int offset, int count) { lock (@lock) { if (count < 0) { throw new java.lang.StringIndexOutOfBoundsException(str, offset, count); } if (str == null) { throw new System.ArgumentNullException("str == null"); } if ((offset | count) < 0 || offset > str.Length - count) { throw new java.lang.StringIndexOutOfBoundsException(str, offset, count); } checkStatus(); java.nio.CharBuffer chars = java.nio.CharBuffer.wrap(java.lang.CharSequenceProxy.Wrap (str), offset, count + offset); convert(chars); } }
public virtual void flush() { //Log.i("PackageManager", "flush mPos=" + mPos); if (mPos > 0) { if (mOutputStream != null) { java.nio.CharBuffer charBuffer = java.nio.CharBuffer.wrap(mText, 0, mPos); java.nio.charset.CoderResult result = mCharset.encode(charBuffer, mBytes, true); while (true) { if (result.isError()) { throw new System.IO.IOException(result.ToString()); } else { if (result.isOverflow()) { flushBytes(); result = mCharset.encode(charBuffer, mBytes, true); continue; } } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText, 0, mPos); mWriter.flush(); } mPos = 0; } }
public sealed override java.nio.CharBuffer put(java.nio.CharBuffer src) { throw new java.nio.ReadOnlyBufferException(); }
public override int read(char[] buffer, int offset, int length) { lock (@lock) { if (!isOpen()) { throw new System.IO.IOException("InputStreamReader is closed"); } java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, length); if (length == 0) { return(0); } java.nio.CharBuffer @out = java.nio.CharBuffer.wrap(buffer, 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 (@out.hasRemaining()) { // fill the buffer if needed if (needInput) { try { if (@in.available() == 0 && @out.position() > offset) { // we could return the result without blocking read break; } } catch (System.IO.IOException) { } // available didn't work so just try the read int desiredByteCount = bytes.capacity() - bytes.limit(); int off = bytes.arrayOffset() + bytes.limit(); int actualByteCount = @in.read(((byte[])bytes.array()), off, desiredByteCount); if (actualByteCount == -1) { endOfInput = true; break; } else { if (actualByteCount == 0) { break; } } bytes.limit(bytes.limit() + actualByteCount); needInput = false; } // decode bytes result = decoder.decode(bytes, @out, 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, @out, true); decoder.flush(@out); decoder.reset(); } if (result.isMalformed() || result.isUnmappable()) { result.throwException(); } return(@out.position() - offset == 0 ? -1 : @out.position() - offset); } }
/// <summary> /// Decodes bytes starting at the current position of the given input buffer, /// and writes the equivalent character sequence into the given output buffer /// from its current position. /// </summary> /// <remarks> /// Decodes bytes starting at the current position of the given input buffer, /// and writes the equivalent character 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> /// <see cref="CoderResult.OVERFLOW">CoderResult.OVERFLOW</see> /// indicates that /// even though not all of the input has been processed, the buffer the /// output is being written to has reached its capacity. In the event of this /// code being returned this method should be called once more with an /// <code>out</code> argument that has not already been filled.</li> /// <li> /// <see cref="CoderResult.UNDERFLOW">CoderResult.UNDERFLOW</see> /// indicates that /// as many bytes as possible in the input buffer have been decoded. If there /// is no further input and no remaining bytes in the input buffer then this /// operation may be regarded as complete. Otherwise, this method should be /// called once more with additional input.</li> /// <li>A /// <see cref="CoderResult.malformedForLength(int)">malformed input</see> /// result /// indicates that some malformed input error has been encountered, and the /// erroneous bytes start at the input buffer's position and their number can /// be got by result's /// <see cref="CoderResult.length()">length</see> /// . This kind of /// result can be returned only if the malformed action is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// . </li> /// <li>A /// <see cref="CoderResult.unmappableForLength(int)">unmappable character</see> /// result indicates that some unmappable character error has been /// encountered, and the erroneous bytes start at the input buffer's position /// and their number can be got by result's /// <see cref="CoderResult.length()">length</see> /// . This kind of result can be returned /// only if the unmappable character action is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// . </li> /// </ul> /// <p> /// The <code>endOfInput</code> parameter indicates that the invoker cannot /// provide further input. This parameter is true if and only if the bytes in /// current input buffer are all inputs for this decoding operation. Note /// that it is common and won't cause an error if the invoker sets false and /// then can't provide more input, 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. /// <p> /// This method invokes the /// <see cref="decodeLoop(java.nio.ByteBuffer, java.nio.CharBuffer)">decodeLoop</see> /// method to /// implement the basic decode logic for a specific charset. /// </remarks> /// <param name="in">the input buffer.</param> /// <param name="out">the output buffer.</param> /// <param name="endOfInput">true if all the input characters have been provided.</param> /// <returns> /// a <code>CoderResult</code> instance which indicates the reason /// of termination. /// </returns> /// <exception cref="System.InvalidOperationException"> /// if decoding has started or no more input is needed in this /// decoding progress. /// </exception> /// <exception cref="CoderMalfunctionError"> /// if the /// <see cref="decodeLoop(java.nio.ByteBuffer, java.nio.CharBuffer)">decodeLoop</see> /// method threw an <code>BufferUnderflowException</code> or /// <code>BufferOverflowException</code>. /// </exception> public java.nio.charset.CoderResult decode(java.nio.ByteBuffer @in, java.nio.CharBuffer @out, bool endOfInput) { if ((status == FLUSH) || (!endOfInput && status == END)) { throw new System.InvalidOperationException(); } java.nio.charset.CoderResult result = null; // begin to decode while (true) { java.nio.charset.CodingErrorAction action = null; try { result = decodeLoop(@in, @out); } catch (java.nio.BufferOverflowException ex) { // unexpected exception throw new java.nio.charset.CoderMalfunctionError(ex); } catch (java.nio.BufferUnderflowException ex) { // unexpected exception throw new java.nio.charset.CoderMalfunctionError(ex); } if (result.isUnderflow()) { int remaining = @in.remaining(); status = endOfInput ? END : ONGOING; if (endOfInput && remaining > 0) { result = java.nio.charset.CoderResult.malformedForLength(remaining); } else { return(result); } } if (result.isOverflow()) { return(result); } // set coding error handle action action = _malformedInputAction; if (result.isUnmappable()) { action = _unmappableCharacterAction; } // If the action is IGNORE or REPLACE, we should continue decoding. if (action == java.nio.charset.CodingErrorAction.REPLACE) { if (@out.remaining() < replacementChars.Length) { return(java.nio.charset.CoderResult.OVERFLOW); } @out.put(replacementChars); } else { if (action != java.nio.charset.CodingErrorAction.IGNORE) { return(result); } } @in.position(@in.position() + result.length()); } }
protected abstract global::java.nio.charset.CoderResult decodeLoop(java.nio.ByteBuffer arg0, java.nio.CharBuffer arg1);
/// <summary>Flushes this decoder.</summary> /// <remarks> /// Flushes this decoder. The default implementation does nothing and always /// returns <code>CoderResult.UNDERFLOW</code>; this method can be /// overridden if needed. /// </remarks> /// <param name="out">the output buffer.</param> /// <returns> /// <code>CoderResult.UNDERFLOW</code> or /// <code>CoderResult.OVERFLOW</code>. /// </returns> protected internal virtual java.nio.charset.CoderResult implFlush(java.nio.CharBuffer @out) { return(java.nio.charset.CoderResult.UNDERFLOW); }
/// <summary>This is a facade method for the encoding operation.</summary> /// <remarks> /// This is a facade method for the encoding operation. /// <p> /// This method encodes the remaining character sequence of the given /// character buffer into a new byte buffer. This method performs a complete /// encoding operation, resets at first, then encodes, and flushes at last. /// <p> /// This method should not be invoked if another encode operation is ongoing. /// </remarks> /// <param name="in">the input buffer.</param> /// <returns> /// a new <code>ByteBuffer</code> containing the bytes produced by /// this encoding operation. The buffer's limit will be the position /// of the last byte in the buffer, and the position will be zero. /// </returns> /// <exception cref="System.InvalidOperationException">if another encoding operation is ongoing. /// </exception> /// <exception cref="MalformedInputException"> /// if an illegal input character sequence for this charset is /// encountered, and the action for malformed error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// </exception> /// <exception cref="UnmappableCharacterException"> /// if a legal but unmappable input character sequence for this /// charset is encountered, and the action for unmappable /// character error is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// . /// Unmappable means the Unicode character sequence at the input /// buffer's current position cannot be mapped to a equivalent /// byte sequence. /// </exception> /// <exception cref="CharacterCodingException">if other exception happened during the encode operation. /// </exception> /// <exception cref="java.nio.charset.CharacterCodingException"></exception> public java.nio.ByteBuffer encode(java.nio.CharBuffer @in) { if (@in.remaining() == 0) { return(java.nio.ByteBuffer.allocate(0)); } reset(); int length = (int)(@in.remaining() * _averageBytesPerChar); java.nio.ByteBuffer output = java.nio.ByteBuffer.allocate(length); java.nio.charset.CoderResult result = null; while (true) { result = encode(@in, output, false); if (result == java.nio.charset.CoderResult.UNDERFLOW) { break; } else { if (result == java.nio.charset.CoderResult.OVERFLOW) { output = allocateMore(output); continue; } } checkCoderResult(result); } result = encode(@in, output, true); checkCoderResult(result); while (true) { result = flush(output); if (result == java.nio.charset.CoderResult.UNDERFLOW) { output.flip(); break; } else { if (result == java.nio.charset.CoderResult.OVERFLOW) { output = allocateMore(output); continue; } } checkCoderResult(result); output.flip(); if (result.isMalformed()) { throw new java.nio.charset.MalformedInputException(result.length()); } else { if (result.isUnmappable()) { throw new java.nio.charset.UnmappableCharacterException(result.length()); } } break; } status = READY; finished = true; return(output); }
/// <summary>Encodes characters into bytes.</summary> /// <remarks> /// Encodes characters into bytes. This method is called by /// <see cref="encode(java.nio.CharBuffer, java.nio.ByteBuffer, bool)">encode</see> /// . /// <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 /// <see cref="encode(java.nio.CharBuffer, java.nio.ByteBuffer, bool)">encode</see> /// . When an /// exception is encountered in the encoding operation, most implementations /// of this method will return a relevant result object to the /// <see cref="encode(java.nio.CharBuffer, java.nio.ByteBuffer, bool)">encode</see> /// 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 /// <see cref="java.nio.Buffer.remaining()">in.remaining()</see> /// characters /// will be read, and /// <see cref="java.nio.Buffer.remaining()">out.remaining()</see> /// 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> /// </remarks> /// <param name="in">the input buffer.</param> /// <param name="out">the output buffer.</param> /// <returns>a <code>CoderResult</code> instance indicating the result.</returns> protected internal abstract java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer @in, java.nio.ByteBuffer @out);
/// <summary> /// 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. /// </summary> /// <remarks> /// 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 /// <see cref="CoderResult.malformedForLength(int)">malformed input</see> /// 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 /// <see cref="CoderResult.length()">length</see> /// . This /// kind of result can be returned only if the malformed action is /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// .</li> /// <li> /// <see cref="CoderResult.UNDERFLOW">CoderResult.UNDERFLOW</see> /// 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> /// <see cref="CoderResult.OVERFLOW">CoderResult.OVERFLOW</see> /// 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 /// <see cref="CoderResult.unmappableForLength(int)">unmappable character</see> /// 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 /// <see cref="CoderResult.length()">length</see> /// . /// This kind of result can be returned only on /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see> /// .</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 /// <see cref="encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer)">encodeLoop</see> /// method to /// implement the basic encode logic for a specific charset. /// </remarks> /// <param name="in">the input buffer.</param> /// <param name="out">the output buffer.</param> /// <param name="endOfInput">true if all the input characters have been provided.</param> /// <returns>a <code>CoderResult</code> instance indicating the result.</returns> /// <exception cref="System.InvalidOperationException"> /// if the encoding operation has already started or no more /// input is needed in this encoding process. /// </exception> /// <exception cref="CoderMalfunctionError"> /// If the /// <see cref="encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer)">encodeLoop</see> /// method threw an <code>BufferUnderflowException</code> or /// <code>BufferUnderflowException</code>. /// </exception> public java.nio.charset.CoderResult encode(java.nio.CharBuffer @in, java.nio.ByteBuffer @out, 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 System.InvalidOperationException(); } if ((status == FLUSH) || (!endOfInput && status == END)) { throw new System.InvalidOperationException(); } java.nio.charset.CoderResult result; while (true) { try { result = encodeLoop(@in, @out); } catch (java.nio.BufferOverflowException e) { throw new java.nio.charset.CoderMalfunctionError(e); } catch (java.nio.BufferUnderflowException e) { throw new java.nio.charset.CoderMalfunctionError(e); } if (result == java.nio.charset.CoderResult.UNDERFLOW) { status = endOfInput ? END : ONGOING; if (endOfInput) { int remaining = @in.remaining(); if (remaining > 0) { result = java.nio.charset.CoderResult.malformedForLength(remaining); } else { return(result); } } else { return(result); } } else { if (result == java.nio.charset.CoderResult.OVERFLOW) { status = endOfInput ? END : ONGOING; return(result); } } java.nio.charset.CodingErrorAction action = _malformedInputAction; if (result.isUnmappable()) { action = _unmappableCharacterAction; } // If the action is IGNORE or REPLACE, we should continue // encoding. if (action == java.nio.charset.CodingErrorAction.REPLACE) { if (@out.remaining() < replacementBytes.Length) { return(java.nio.charset.CoderResult.OVERFLOW); } @out.put(replacementBytes); } else { if (action != java.nio.charset.CodingErrorAction.IGNORE) { return(result); } } @in.position(@in.position() + result.length()); } }