internal virtual sbyte[] Encode(char[] ca, int off, int len) { int en = Scale(len, Ce.MaxBytesPerChar()); sbyte[] ba = new sbyte[en]; if (len == 0) { return(ba); } if (Ce is ArrayEncoder) { int blen = ((ArrayEncoder)Ce).encode(ca, off, len, ba); return(SafeTrim(ba, blen, Cs, IsTrusted)); } else { Ce.Reset(); ByteBuffer bb = ByteBuffer.Wrap(ba); CharBuffer cb = CharBuffer.Wrap(ca, off, len); try { CoderResult cr = Ce.Encode(cb, bb, true); if (!cr.Underflow) { cr.ThrowException(); } cr = Ce.Flush(bb); if (!cr.Underflow) { cr.ThrowException(); } } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return(SafeTrim(ba, bb.Position(), Cs, IsTrusted)); } }
/// <summary> /// Constructs a writer that encodes characters using the given encoder and /// writes the resulting bytes to the given channel. /// /// <para> The resulting stream will contain an internal output buffer of at /// least <tt>minBufferCap</tt> bytes. The stream's <tt>write</tt> methods /// will, as needed, flush the buffer by writing bytes to the underlying /// channel; if the channel is in non-blocking mode when bytes are to be /// written then an <seealso cref="IllegalBlockingModeException"/> will be thrown. /// The resulting stream will not otherwise be buffered. Closing the stream /// will in turn cause the channel to be closed. </para> /// </summary> /// <param name="ch"> /// The channel to which bytes will be written /// </param> /// <param name="enc"> /// The charset encoder to be used /// </param> /// <param name="minBufferCap"> /// The minimum capacity of the internal byte buffer, /// or <tt>-1</tt> if an implementation-dependent /// default capacity is to be used /// </param> /// <returns> A new writer </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static java.io.Writer newWriter(final WritableByteChannel ch, final java.nio.charset.CharsetEncoder enc, final int minBufferCap) public static Writer NewWriter(WritableByteChannel ch, CharsetEncoder enc, int minBufferCap) { CheckNotNull(ch, "ch"); return(StreamEncoder.forEncoder(ch, enc.Reset(), minBufferCap)); }