// implementation of canEncode private bool implCanEncode(CharBuffer cb) { if (status == FLUSH || status == INIT) { status = READY; } if (status != READY) { // niochar.0B=Another encoding process is ongoing\! throw new java.lang.IllegalStateException("Another encoding process is ongoing!"); //$NON-NLS-1$ } CodingErrorAction malformBak = malformAction; CodingErrorAction unmapBak = unmapAction; onMalformedInput(CodingErrorAction.REPORT); onUnmappableCharacter(CodingErrorAction.REPORT); bool result = true; try { this.encode(cb); } catch (CharacterCodingException e) { result = false; } onMalformedInput(malformBak); onUnmappableCharacter(unmapBak); reset(); return(result); }
/* * Sets this decoder's action on malformed input errors. * * This method will call the * {@link #implOnMalformedInput(CodingErrorAction) implOnMalformedInput} * method with the given new action as argument. * * @param newAction * the new action on malformed input error. * @return this decoder. * @throws IllegalArgumentException * if {@code newAction} is {@code null}. */ public CharsetDecoder onMalformedInput(CodingErrorAction newAction) { if (null == newAction) { throw new java.lang.IllegalArgumentException(); } malformAction = newAction; implOnMalformedInput(newAction); return(this); }
/* * Sets this encoder's action on malformed input error. * * This method will call the * {@link #implOnMalformedInput(CodingErrorAction) implOnMalformedInput} * method with the given new action as argument. * * @param newAction * the new action on malformed input error. * @return this encoder. * @throws IllegalArgumentException * if the given newAction is null. */ public CharsetEncoder onMalformedInput(CodingErrorAction newAction) { if (null == newAction) { // niochar.0C=Action on malformed input error cannot be null\! throw new java.lang.IllegalArgumentException("Action on malformed input error cannot be null!"); //$NON-NLS-1$ } malformAction = newAction; implOnMalformedInput(newAction); return(this); }
/* * Sets this decoder's action on unmappable character errors. * * This method will call the * {@link #implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter} * method with the given new action as argument. * * @param newAction * the new action on unmappable character error. * @return this decoder. * @throws IllegalArgumentException * if {@code newAction} is {@code null}. */ public CharsetDecoder onUnmappableCharacter( CodingErrorAction newAction) { if (null == newAction) { throw new java.lang.IllegalArgumentException(); } unmapAction = newAction; implOnUnmappableCharacter(newAction); return(this); }
/* * Sets this encoder's action on unmappable character error. * * This method will call the * {@link #implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter} * method with the given new action as argument. * * @param newAction * the new action on unmappable character error. * @return this encoder. * @throws IllegalArgumentException * if the given newAction is null. */ public CharsetEncoder onUnmappableCharacter( CodingErrorAction newAction) { if (null == newAction) { // niochar.0D=Action on unmappable character error cannot be null\! throw new java.lang.IllegalArgumentException("Action on unmappable character error cannot be null!"); //$NON-NLS-1$ } unmapAction = newAction; implOnUnmappableCharacter(newAction); return(this); }
/* * Constructs a new <code>CharsetDecoder</code> using the given * <code>Charset</code>, average number and maximum number of characters * created by this decoder for one input byte, and the default replacement * string "\uFFFD". * * @param charset * the <code>Charset</code> to be used by this decoder. * @param averageCharsPerByte * the average number of characters created by this decoder for * one input byte, must be positive. * @param maxCharsPerByte * the maximum number of characters created by this decoder for * one input byte, must be positive. * @throws IllegalArgumentException * if <code>averageCharsPerByte</code> or * <code>maxCharsPerByte</code> is negative. */ protected CharsetDecoder(Charset charset, float averageCharsPerByte, float maxCharsPerByte) { if (averageCharsPerByte <= 0 || maxCharsPerByte <= 0) { // niochar.00=Characters number for one byte must be positive. throw new java.lang.IllegalArgumentException("Characters number for one byte must be positive."); //$NON-NLS-1$ } if (averageCharsPerByte > maxCharsPerByte) { // niochar.01=averageCharsPerByte is greater than maxCharsPerByte throw new java.lang.IllegalArgumentException("averageCharsPerByte is greater than maxCharsPerByte"); //$NON-NLS-1$ } averChars = averageCharsPerByte; maxChars = maxCharsPerByte; cs = charset; status = INIT; malformAction = CodingErrorAction.REPORT; unmapAction = CodingErrorAction.REPORT; replace = "\ufffd"; //$NON-NLS-1$ }
/* * 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()); }
/* * Constructs a new <code>CharsetEncoder</code> using the given * <code>Charset</code>, replacement byte array, average number and * maximum number of bytes created by this encoder for one input character. * * @param cs * the <code>Charset</code> to be used by this encoder. * @param averageBytesPerChar * average number of bytes created by this encoder for one single * input character, must be positive. * @param maxBytesPerChar * maximum number of bytes which can be created by this encoder * for one single input character, must be positive. * @param replacement * the replacement byte array, cannot be null or empty, its * length cannot be larger than <code>maxBytesPerChar</code>, * and must be a legal replacement, which can be justified by * {@link #isLegalReplacement(byte[]) isLegalReplacement}. * @throws IllegalArgumentException * if any parameters are invalid. */ protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement) { if (averageBytesPerChar <= 0 || maxBytesPerChar <= 0) { // niochar.02=Bytes number for one character must be positive. throw new java.lang.IllegalArgumentException("Bytes number for one character must be positive."); //$NON-NLS-1$ } if (averageBytesPerChar > maxBytesPerChar) { // niochar.03=averageBytesPerChar is greater than maxBytesPerChar. throw new java.lang.IllegalArgumentException("averageBytesPerChar is greater than maxBytesPerChar."); //$NON-NLS-1$ } this.cs = cs; averBytes = averageBytesPerChar; maxBytes = maxBytesPerChar; status = INIT; malformAction = CodingErrorAction.REPORT; unmapAction = CodingErrorAction.REPORT; replaceWith(replacement); }
/** * Sets this decoder's action on unmappable character errors. * * This method will call the * {@link #implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter} * method with the given new action as argument. * * @param newAction * the new action on unmappable character error. * @return this decoder. * @throws IllegalArgumentException * if {@code newAction} is {@code null}. */ public CharsetDecoder onUnmappableCharacter( CodingErrorAction newAction) { if (null == newAction) { throw new java.lang.IllegalArgumentException(); } unmapAction = newAction; implOnUnmappableCharacter(newAction); return this; }
/* * Notifies that this encoder's <code>CodingErrorAction</code> specified * for unmappable character error has been changed. The default * implementation does nothing; this method can be overridden if needed. * * @param newAction * the new action. */ protected void implOnUnmappableCharacter(CodingErrorAction newAction) { // default implementation is empty }
/* * Notifies that this encoder's <code>CodingErrorAction</code> specified * for malformed input error has been changed. The default implementation * does nothing; this method can be overridden if needed. * * @param newAction * the new action. */ protected void implOnMalformedInput(CodingErrorAction newAction) { // default implementation is empty }
/* * 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()); } }
/** * Sets this encoder's action on unmappable character error. * * This method will call the * {@link #implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter} * method with the given new action as argument. * * @param newAction * the new action on unmappable character error. * @return this encoder. * @throws IllegalArgumentException * if the given newAction is null. */ public CharsetEncoder onUnmappableCharacter( CodingErrorAction newAction) { if (null == newAction) { // niochar.0D=Action on unmappable character error cannot be null\! throw new java.lang.IllegalArgumentException("Action on unmappable character error cannot be null!"); //$NON-NLS-1$ } unmapAction = newAction; implOnUnmappableCharacter(newAction); return this; }
/** * Notifies that this decoder's <code>CodingErrorAction</code> specified * for unmappable character error has been changed. The default * implementation does nothing; this method can be overridden if needed. * * @param newAction * the new action. */ protected void implOnUnmappableCharacter(CodingErrorAction newAction) { // default implementation is empty }
/** * Sets this encoder's action on malformed input error. * * This method will call the * {@link #implOnMalformedInput(CodingErrorAction) implOnMalformedInput} * method with the given new action as argument. * * @param newAction * the new action on malformed input error. * @return this encoder. * @throws IllegalArgumentException * if the given newAction is null. */ public CharsetEncoder onMalformedInput(CodingErrorAction newAction) { if (null == newAction) { // niochar.0C=Action on malformed input error cannot be null\! throw new java.lang.IllegalArgumentException("Action on malformed input error cannot be null!"); //$NON-NLS-1$ } malformAction = newAction; implOnMalformedInput(newAction); return this; }
/* * 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>{@link CoderResult#OVERFLOW CoderResult.OVERFLOW} 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>{@link CoderResult#UNDERFLOW CoderResult.UNDERFLOW} 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 {@link CoderResult#malformedForLength(int) malformed input} 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 {@link CoderResult#length() length}. This kind of * result can be returned only if the malformed action is * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}. </li> * <li>A {@link CoderResult#unmappableForLength(int) unmappable character} * 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 * {@link CoderResult#length() length}. This kind of result can be returned * only if the unmappable character action is * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}. </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 * {@link #decodeLoop(ByteBuffer, CharBuffer) decodeLoop} method to * implement the basic decode 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 which indicates the reason * of termination. * @throws IllegalStateException * if decoding has started or no more input is needed in this * decoding progress. * @throws CoderMalfunctionError * if the {@link #decodeLoop(ByteBuffer, CharBuffer) decodeLoop} * method threw an <code>BufferUnderflowException</code> or * <code>BufferOverflowException</code>. */ public CoderResult decode(ByteBuffer inJ, CharBuffer outJ, bool endOfInput) { /* * status check */ if ((status == FLUSH) || (!endOfInput && status == END)) { throw new java.lang.IllegalStateException(); } CoderResult result = null; // begin to decode while (true) { CodingErrorAction action = null; try { result = decodeLoop(inJ, outJ); } catch (BufferOverflowException ex) { // unexpected exception throw new CoderMalfunctionError(ex); } catch (BufferUnderflowException ex) { // unexpected exception throw new CoderMalfunctionError(ex); } /* * result handling */ if (result.isUnderflow()) { int remaining = inJ.remaining(); // WHY inJ.remaining() == 1? status = endOfInput ? END : ONGOING; if (endOfInput && remaining > 0) { result = CoderResult.malformedForLength(remaining); } else { return(result); } } if (result.isOverflow()) { return(result); } // set coding error handle action action = malformAction; if (result.isUnmappable()) { action = unmapAction; } // If the action is IGNORE or REPLACE, we should continue decoding. 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()); } }
/** * Sets this decoder's action on malformed input errors. * * This method will call the * {@link #implOnMalformedInput(CodingErrorAction) implOnMalformedInput} * method with the given new action as argument. * * @param newAction * the new action on malformed input error. * @return this decoder. * @throws IllegalArgumentException * if {@code newAction} is {@code null}. */ public CharsetDecoder onMalformedInput(CodingErrorAction newAction) { if (null == newAction) { throw new java.lang.IllegalArgumentException(); } malformAction = newAction; implOnMalformedInput(newAction); return this; }
/** * Constructs a new <code>CharsetDecoder</code> using the given * <code>Charset</code>, average number and maximum number of characters * created by this decoder for one input byte, and the default replacement * string "\uFFFD". * * @param charset * the <code>Charset</code> to be used by this decoder. * @param averageCharsPerByte * the average number of characters created by this decoder for * one input byte, must be positive. * @param maxCharsPerByte * the maximum number of characters created by this decoder for * one input byte, must be positive. * @throws IllegalArgumentException * if <code>averageCharsPerByte</code> or * <code>maxCharsPerByte</code> is negative. */ protected CharsetDecoder(Charset charset, float averageCharsPerByte, float maxCharsPerByte) { if (averageCharsPerByte <= 0 || maxCharsPerByte <= 0) { // niochar.00=Characters number for one byte must be positive. throw new java.lang.IllegalArgumentException("Characters number for one byte must be positive."); //$NON-NLS-1$ } if (averageCharsPerByte > maxCharsPerByte) { // niochar.01=averageCharsPerByte is greater than maxCharsPerByte throw new java.lang.IllegalArgumentException("averageCharsPerByte is greater than maxCharsPerByte"); //$NON-NLS-1$ } averChars = averageCharsPerByte; maxChars = maxCharsPerByte; cs = charset; status = INIT; malformAction = CodingErrorAction.REPORT; unmapAction = CodingErrorAction.REPORT; replace = "\ufffd"; //$NON-NLS-1$ }
/** * Notifies that this decoder's <code>CodingErrorAction</code> specified * for malformed input error has been changed. The default implementation * does nothing; this method can be overridden if needed. * * @param newAction * the new action. */ protected void implOnMalformedInput(CodingErrorAction newAction) { // default implementation is empty }
/** * Constructs a new <code>CharsetEncoder</code> using the given * <code>Charset</code>, replacement byte array, average number and * maximum number of bytes created by this encoder for one input character. * * @param cs * the <code>Charset</code> to be used by this encoder. * @param averageBytesPerChar * average number of bytes created by this encoder for one single * input character, must be positive. * @param maxBytesPerChar * maximum number of bytes which can be created by this encoder * for one single input character, must be positive. * @param replacement * the replacement byte array, cannot be null or empty, its * length cannot be larger than <code>maxBytesPerChar</code>, * and must be a legal replacement, which can be justified by * {@link #isLegalReplacement(byte[]) isLegalReplacement}. * @throws IllegalArgumentException * if any parameters are invalid. */ protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement) { if (averageBytesPerChar <= 0 || maxBytesPerChar <= 0) { // niochar.02=Bytes number for one character must be positive. throw new java.lang.IllegalArgumentException("Bytes number for one character must be positive."); //$NON-NLS-1$ } if (averageBytesPerChar > maxBytesPerChar) { // niochar.03=averageBytesPerChar is greater than maxBytesPerChar. throw new java.lang.IllegalArgumentException("averageBytesPerChar is greater than maxBytesPerChar."); //$NON-NLS-1$ } this.cs = cs; averBytes = averageBytesPerChar; maxBytes = maxBytesPerChar; status = INIT; malformAction = CodingErrorAction.REPORT; unmapAction = CodingErrorAction.REPORT; replaceWith(replacement); }