com.beetstra.jutf7
Class UTF7StyleCharsetEncoder

java.lang.Object
  extended by java.nio.charset.CharsetEncoder
      extended by com.beetstra.jutf7.UTF7StyleCharsetEncoder

 class UTF7StyleCharsetEncoder
extends CharsetEncoder

The CharsetEncoder used to encode both variants of the UTF-7 charset and the modified-UTF-7 charset.

Please note this class does not behave strictly according to the specification in Sun Java VMs before 1.6. This is done to get around a bug in the implementation of CharsetEncoder.encode(CharBuffer). Unfortunately, that method cannot be overridden.

Author:
Jaap Beetstra
See Also:
JDK bug 6221056

Field Summary
(package private) static boolean useUglyHackToForceCallToFlushInJava5
           
 
Constructor Summary
UTF7StyleCharsetEncoder(UTF7StyleCharset cs, Base64Util base64, boolean strict)
           
 
Method Summary
protected  CoderResult encodeLoop(CharBuffer in, ByteBuffer out)
           Note that this method might return CoderResult.OVERFLOW, even though there is sufficient space available in the output buffer.
protected  CoderResult implFlush(ByteBuffer out)
           Note that this method might return CoderResult.OVERFLOW (as is required by the specification) if insufficient space is available in the output buffer.
protected  void implReset()
           
 
Methods inherited from class java.nio.charset.CharsetEncoder
averageBytesPerChar, canEncode, canEncode, charset, encode, encode, flush, implOnMalformedInput, implOnUnmappableCharacter, implReplaceWith, isLegalReplacement, malformedInputAction, maxBytesPerChar, onMalformedInput, onUnmappableCharacter, replacement, replaceWith, reset, unmappableCharacterAction
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useUglyHackToForceCallToFlushInJava5

static boolean useUglyHackToForceCallToFlushInJava5
Constructor Detail

UTF7StyleCharsetEncoder

UTF7StyleCharsetEncoder(UTF7StyleCharset cs,
                        Base64Util base64,
                        boolean strict)
Method Detail

implReset

protected void implReset()
Overrides:
implReset in class CharsetEncoder

implFlush

protected CoderResult implFlush(ByteBuffer out)

Note that this method might return CoderResult.OVERFLOW (as is required by the specification) if insufficient space is available in the output buffer. However, calling it again on JDKs before Java 6 triggers a bug in CharsetEncoder.flush(ByteBuffer) causing it to throw an IllegalStateException (the buggy method is final, thus cannot be overridden).

Overrides:
implFlush in class CharsetEncoder
Parameters:
out - The output byte buffer
Returns:
A coder-result object describing the reason for termination
See Also:
JDK bug 6227608

encodeLoop

protected CoderResult encodeLoop(CharBuffer in,
                                 ByteBuffer out)

Note that this method might return CoderResult.OVERFLOW, even though there is sufficient space available in the output buffer. This is done to force the broken implementation of CharsetEncoder.encode(CharBuffer) to call flush (the buggy method is final, thus cannot be overridden).

However, String.getBytes() fails if CoderResult.OVERFLOW is returned, since this assumes it always allocates sufficient bytes (maxBytesPerChar * nr_of_chars). Thus, as an extra check, the size of the input buffer is compared against the size of the output buffer. A static variable is used to indicate if a broken java version is used.

It is not possible to directly write the last few bytes, since more bytes might be waiting to be encoded then those available in the input buffer.

Specified by:
encodeLoop in class CharsetEncoder
Parameters:
in - The input character buffer
out - The output byte buffer
Returns:
A coder-result object describing the reason for termination
See Also:
JDK bug 6221056


Copyright © 2008 sourceforge. All Rights Reserved.