/** * Pushes the encoded form of the given character to the given byte buffer. * * @param bb The byte buffer to write to. * @param c The character to encode. * @return Whether the given unicode character is covered by this encoding. * If <code>false</code> is returned, nothing is pushed to the * byte buffer. */ public bool pushEncodedChar(java.nio.ByteBuffer bb, char c) { if (c >= 0 && c < 128) { bb.put((byte)c); return(true); } Simple8BitChar r = this.encodeHighChar(c); if (r == null) { return(false); } bb.put(r.code); return(true); }
// allocate more spaces to the given ByteBuffer private java.nio.ByteBuffer allocateMore(java.nio.ByteBuffer output) { if (output.capacity() == 0) { return(java.nio.ByteBuffer.allocate(1)); } java.nio.ByteBuffer result = java.nio.ByteBuffer.allocate(output.capacity() * 2); output.flip(); result.put(output); return(result); }
/** * Grow a byte buffer, so it has a minimal capacity or at least * the double capacity of the original buffer * * @param b The original buffer. * @param newCapacity The minimal requested new capacity. * @return A byte buffer <code>r</code> with * <code>r.capacity() = max(b.capacity()*2,newCapacity)</code> and * all the data contained in <code>b</code> copied to the beginning * of <code>r</code>. * */ internal static java.nio.ByteBuffer growBuffer(java.nio.ByteBuffer b, int newCapacity) { b.limit(b.position()); b.rewind(); int c2 = b.capacity() * 2; java.nio.ByteBuffer on = java.nio.ByteBuffer.allocate(c2 < newCapacity ? newCapacity : c2); on.put(b); return(on); }
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); }
/** * Append <code>%Uxxxx</code> to the given byte buffer. * The caller must assure, that <code>bb.remaining()>=6</code>. * * @param bb The byte buffer to write to. * @param c The character to write. */ internal static void appendSurrogate(java.nio.ByteBuffer bb, char c) { bb.put((byte)'%'); bb.put((byte)'U'); bb.put(HEX_DIGITS[(c >> 12) & 0x0f]); bb.put(HEX_DIGITS[(c >> 8) & 0x0f]); bb.put(HEX_DIGITS[(c >> 4) & 0x0f]); bb.put(HEX_DIGITS[c & 0x0f]); }
/* * 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()); } }