Java Doc for CharsetEncoder.java in  » Apache-Harmony-Java-SE » java-package » java » nio » charset » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.nio.charset 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.charset.CharsetEncoder

CharsetEncoder
abstract public class CharsetEncoder (Code)
An converter that can convert 16-bit Unicode character sequence to byte sequence in some charset .

The input character sequence is wrapped by java.nio.CharBuffer CharBuffer and the output character sequence is java.nio.ByteBuffer ByteBuffer . A encoder instance should be used in following sequence, which is referred to as a encoding operation:

  1. Invoking the CharsetEncoder.reset() reset method to reset the encoder if the encoder has been used;
  2. Invoking the CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode method until the additional input is not needed, the endOfInput parameter must be set to false, the input buffer must be filled and the output buffer must be flushed between invocations;
  3. Invoking the CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode method last time, and the the endOfInput parameter must be set to true
  4. Invoking the CharsetEncoder.flush(ByteBuffer) flush method to flush the output.

The CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode method will convert as many characters as possible, and the process won't stop except the input characters has been run out of, the output buffer has been filled or some error has happened. A CoderResult CoderResult instance will be returned to indicate the stop reason, and the invoker can identify the result and choose further action, which can include filling the input buffer, flushing the output buffer, recovering from error and trying again.

There are two common encoding errors. One is named as malformed and it is returned when the input content is illegal 16-bit Unicode character sequence, the other is named as unmappable character and occurs when there is a problem mapping the input to a valid byte sequence in the specific charset.

The two errors can be handled in three ways, the default one is to report the error to the invoker by a CoderResult CoderResult instance, and the alternatives are to ignore it or to replace the erroneous input with the replacement byte array. The replacement byte array is {(byte)'?'} by default and can be changed by invoking CharsetEncoder.replaceWith(byte[]) replaceWith method. The invoker of this encoder can choose one way by specifying a CodingErrorAction CodingErrorAction instance for each error type via CharsetEncoder.onMalformedInput(CodingErrorAction) onMalformedInput method and CharsetEncoder.onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter method.

This class is abstract class and encapsulate many common operations of encoding process for all charsets. encoder for specific charset should extend this class and need only implement CharsetEncoder.encodeLoop(CharBuffer,ByteBuffer) encodeLoop method for basic encoding loop. If a subclass maintains internal state, it should override the CharsetEncoder.implFlush(ByteBuffer) implFlush method and CharsetEncoder.implReset() implReset method in addition.

This class is not thread-safe.


See Also:   java.nio.charset.Charset
See Also:   java.nio.charset.CharsetDecoder



Constructor Summary
protected  CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar)
     Construct a new CharsetEncoder using given Charset, average number and maximum number of bytes created by this encoder for one input character.
protected  CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement)
     Construct a new CharsetEncoder using given Charset, replace byte array, average number and maximum number of bytes created by this encoder for one input character.

Method Summary
final public  floataverageBytesPerChar()
    
public  booleancanEncode(char c)
     Check if given character can be encoded by this encoder.
public  booleancanEncode(CharSequence sequence)
     Check if given CharSequence can be encoded by this encoder.
final public  Charsetcharset()
     Get the Charset which creates this encoder.
final public  ByteBufferencode(CharBuffer in)
     This is a facade method for encoding operation.

This method encodes the remaining character sequence of the given character buffer into a new byte buffer.

final public  CoderResultencode(CharBuffer in, ByteBuffer out, boolean endOfInput)
     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.

The buffers' position will be changed with the reading and writing operation, but their limits and marks will be kept intact.

A CoderResult instance will be returned according to following rules:

abstract protected  CoderResultencodeLoop(CharBuffer in, ByteBuffer out)
     Encode characters into bytes.
final public  CoderResultflush(ByteBuffer out)
     Flush this encoder. This method will call CharsetEncoder.implFlush(ByteBuffer) implFlush .
protected  CoderResultimplFlush(ByteBuffer out)
     Flush this encoder.
protected  voidimplOnMalformedInput(CodingErrorAction newAction)
     Notify that this encoder's CodingErrorAction specified for malformed input error has been changed.
protected  voidimplOnUnmappableCharacter(CodingErrorAction newAction)
     Notify that this encoder's CodingErrorAction specified for unmappable character error has been changed.
protected  voidimplReplaceWith(byte[] newReplacement)
     Notify that this encoder's replacement has been changed.
protected  voidimplReset()
     Reset this encoder's charset related state.
public  booleanisLegalReplacement(byte[] repl)
     Check if the given argument is legal as this encoder's replacement byte array.
public  CodingErrorActionmalformedInputAction()
     Gets this encoder's CodingErrorAction when malformed input occurred during encoding process.
final public  floatmaxBytesPerChar()
    
final public  CharsetEncoderonMalformedInput(CodingErrorAction newAction)
     Set this encoder's action on malformed input error.
final public  CharsetEncoderonUnmappableCharacter(CodingErrorAction newAction)
     Set this encoder's action on unmappable character error.
final public  CharsetEncoderreplaceWith(byte[] replacement)
     Set new replacement value.
final public  byte[]replacement()
    
final public  CharsetEncoderreset()
     Reset this encoder.
public  CodingErrorActionunmappableCharacterAction()
     Gets this encoder's CodingErrorAction when unmappable character occurred during encoding process.


Constructor Detail
CharsetEncoder
protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar)(Code)
Construct a new CharsetEncoder using given Charset, average number and maximum number of bytes created by this encoder for one input character.
Parameters:
  cs - this encoder's Charset, which create thisencoder
Parameters:
  averageBytesPerChar - average number of bytes created by this encoder for one inputcharacter, must be positive
Parameters:
  maxBytesPerChar - maximum number of bytes which can be created by this encoderfor one input character, must be positive
throws:
  IllegalArgumentException - if maxBytesPerChar oraverageBytePerChar is negative



CharsetEncoder
protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement)(Code)
Construct a new CharsetEncoder using given Charset, replace byte array, average number and maximum number of bytes created by this encoder for one input character.
Parameters:
  cs - the this encoder's Charset, which create thisencoder
Parameters:
  averageBytesPerChar - average number of bytes created by this encoder for singleinput character, must be positive
Parameters:
  maxBytesPerChar - maximum number of bytes which can be created by this encoderfor single input character, must be positive
Parameters:
  replacement - the replacement byte array, cannot be null or empty, itslength cannot larger than maxBytesPerChar, andmust be legal replacement, which can be justified byCharsetEncoder.isLegalReplacement(byte[]) isLegalReplacement
throws:
  IllegalArgumentException - if any parameters are invalid




Method Detail
averageBytesPerChar
final public float averageBytesPerChar()(Code)
get the average number of bytes created by this encoder for single input character the average number of bytes created by this encoder for singleinput character



canEncode
public boolean canEncode(char c)(Code)
Check if given character can be encoded by this encoder. Note that this method can change the internal status of this encoder, so it should not be called when another encode process is ongoing, otherwise it will throw IllegalStateException. This method can be overridden for performance improvement.
Parameters:
  c - the given encoder true if given character can be encoded by this encoder
throws:
  IllegalStateException - if another encode process is ongoing so that current internalstatus is neither RESET or FLUSH



canEncode
public boolean canEncode(CharSequence sequence)(Code)
Check if given CharSequence can be encoded by this encoder. Note that this method can change the internal status of this encoder, so it should not be called when another encode process is ongoing, otherwise it will throw IllegalStateException. This method can be overridden for performance improvement.
Parameters:
  sequence - the given CharSequence true if given CharSequence can be encoded by thisencoder
throws:
  IllegalStateException - if current internal status is neither RESET or FLUSH



charset
final public Charset charset()(Code)
Get the Charset which creates this encoder. the Charset which creates this encoder



encode
final public ByteBuffer encode(CharBuffer in) throws CharacterCodingException(Code)
This is a facade method for encoding operation.

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.

This method should not be invoked if another encode operation is ongoing.


Parameters:
  in - the input buffer a new ByteBuffer containing the the bytes producedby this encoding operation. The buffer's limit will be theposition of last byte in buffer, and the position will be zero
throws:
  IllegalStateException - if another encoding operation is ongoing
throws:
  MalformedInputException - if illegal input character sequence for this charsetencountered, and the action for malformed error isCodingErrorAction.REPORT CodingErrorAction.REPORT
throws:
  UnmappableCharacterException - if legal but unmappable input character sequence for thischarset encountered, and the action for unmappable charactererror isCodingErrorAction.REPORT CodingErrorAction.REPORT.Unmappable means the Unicode character sequence at the inputbuffer's current position cannot be mapped to a equivalentbyte sequence.
throws:
  CharacterCodingException - if other exception happened during the encode operation



encode
final public CoderResult encode(CharBuffer in, ByteBuffer out, boolean endOfInput)(Code)
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.

The buffers' position will be changed with the reading and writing operation, but their limits and marks will be kept intact.

A CoderResult instance will be returned according to following rules:

  • A CoderResult.malformedForLength(int) malformed input result indicates that some malformed input error encountered, and the erroneous characters start at the input buffer's position and their number can be got by result's CoderResult.length length . This kind of result can be returned only if the malformed action is CodingErrorAction.REPORT CodingErrorAction.REPORT .
  • CoderResult.UNDERFLOW CoderResult.UNDERFLOW indicates that as many characters as possible in the input buffer has 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.
  • 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
  • A 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 CoderResult.length length . This kind of result can be returned only on CodingErrorAction.REPORT CodingErrorAction.REPORT .

The endOfInput parameter indicates that if the invoker can provider further input. This parameter is true if and only if the characters in current input buffer are all inputs for this encoding operation. Note that it is common and won't cause error that the invoker sets false and then finds no more input available; while it may cause error that the invoker always sets true in several consecutive invocations so that any remaining input will be treated as malformed input.

This method invokes CharsetEncoder.encodeLoop(CharBuffer,ByteBuffer) encodeLoop method to implement basic encode logic for specific charset.


Parameters:
  in - the input buffer
Parameters:
  out - the output buffer
Parameters:
  endOfInput - true if all the input characters have been provided a CoderResult instance indicating the result
throws:
  IllegalStateException - if the encoding operation has already started or no moreinput needed in this encoding progress.
throws:
  CoderMalfunctionError - If the CharsetEncoder.encodeLoop(CharBuffer,ByteBuffer) encodeLoopmethod threw an BufferUnderflowException orBufferUnderflowException



encodeLoop
abstract protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out)(Code)
Encode characters into bytes. This method is called by CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode . 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 encountered. And then it will return a CoderResult object indicating the result of current encoding operation. The rules to construct the CoderResult is same as the CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode . When exception encountered in the encoding operation, most implementation of this method will return a relevant result object to CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode method, and some performance optimized implementation may handle the exception and implement the error action itself. 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 CharBuffer.remaining in.remaining() characters will be read, and ByteBuffer.remaining out.remaining() bytes will be written. Note that some implementation may pre-scan the input buffer and return CoderResult.UNDERFLOW until it receives sufficient input.
Parameters:
  in - the input buffer
Parameters:
  out - the output buffer a CoderResult instance indicating the result



flush
final public CoderResult flush(ByteBuffer out)(Code)
Flush this encoder. This method will call CharsetEncoder.implFlush(ByteBuffer) implFlush . Some encoders may need to write some bytes to the output buffer when they have read all input characters, subclasses can overridden CharsetEncoder.implFlush(ByteBuffer) implFlush to perform writing action. The maximum number of written bytes won't larger than ByteBuffer.remaining out.remaining() . If some encoder want to write more bytes than output buffer's remaining spaces, then CoderResult.OVERFLOW will be returned, and this method must be called again with a byte buffer has more spaces. Otherwise this method will return CoderResult.UNDERFLOW, which means one encoding process has been completed successfully. During the flush, the output buffer's position will be changed accordingly, while its mark and limit will be intact.
Parameters:
  out - the given output buffer CoderResult.UNDERFLOW orCoderResult.OVERFLOW
throws:
  IllegalStateException - if this encoder hasn't read all input characters during oneencoding process, which means neither after callingCharsetEncoder.encode(CharBuffer) encode(CharBuffer) nor aftercalling CharsetEncoder.encode(CharBuffer,ByteBuffer,boolean) encode(CharBuffer, ByteBuffer, boolean) with true value forthe last boolean parameter



implFlush
protected CoderResult implFlush(ByteBuffer out)(Code)
Flush this encoder. Default implementation does nothing and always return CoderResult.UNDERFLOW, and this method can be overridden if needed.
Parameters:
  out - the output buffer CoderResult.UNDERFLOW orCoderResult.OVERFLOW



implOnMalformedInput
protected void implOnMalformedInput(CodingErrorAction newAction)(Code)
Notify that this encoder's CodingErrorAction specified for malformed input error has been changed. Default implementation does nothing, and this method can be overridden if needed.
Parameters:
  newAction - The new action



implOnUnmappableCharacter
protected void implOnUnmappableCharacter(CodingErrorAction newAction)(Code)
Notify that this encoder's CodingErrorAction specified for unmappable character error has been changed. Default implementation does nothing, and this method can be overridden if needed.
Parameters:
  newAction - The new action



implReplaceWith
protected void implReplaceWith(byte[] newReplacement)(Code)
Notify that this encoder's replacement has been changed. Default implementation does nothing, and this method can be overridden if needed.
Parameters:
  newReplacement - the new replacement string



implReset
protected void implReset()(Code)
Reset this encoder's charset related state. Default implementation does nothing, and this method can be overridden if needed.



isLegalReplacement
public boolean isLegalReplacement(byte[] repl)(Code)
Check 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.
Parameters:
  repl - the given byte array to be checked true if the the given argument is legal as this encoder'sreplacement byte array.



malformedInputAction
public CodingErrorAction malformedInputAction()(Code)
Gets this encoder's CodingErrorAction when malformed input occurred during encoding process. this encoder's CodingErrorAction when malformedinput occurred during encoding process.



maxBytesPerChar
final public float maxBytesPerChar()(Code)
Get the maximum number of bytes which can be created by this encoder for one input character, must be positive the maximum number of bytes which can be created by this encoderfor one input character, must be positive



onMalformedInput
final public CharsetEncoder onMalformedInput(CodingErrorAction newAction)(Code)
Set this encoder's action on malformed input error. This method will call the CharsetEncoder.implOnMalformedInput(CodingErrorAction) implOnMalformedInput method with the given new action as argument.
Parameters:
  newAction - the new action on malformed input error this encoder
throws:
  IllegalArgumentException - if the given newAction is null



onUnmappableCharacter
final public CharsetEncoder onUnmappableCharacter(CodingErrorAction newAction)(Code)
Set this encoder's action on unmappable character error. This method will call the CharsetEncoder.implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter method with the given new action as argument.
Parameters:
  newAction - the new action on unmappable character error this encoder
throws:
  IllegalArgumentException - if the given newAction is null



replaceWith
final public CharsetEncoder replaceWith(byte[] replacement)(Code)
Set new replacement value. This method first checks the given replacement's validity, then changes the replacement value, and at last calls CharsetEncoder.implReplaceWith(byte[]) implReplaceWith method with the given new replacement as argument.
Parameters:
  replacement - the replacement byte array, cannot be null or empty, itslength cannot larger than maxBytesPerChar, andmust be legal replacement, which can be justified byisLegalReplacement(byte[] repl) this encoder
throws:
  IllegalArgumentException - if the given replacement cannot satisfy the requirementmentioned above



replacement
final public byte[] replacement()(Code)
Get the replacement byte array, which is never null or empty, and it is legal the replacement byte array, cannot be null or empty, and it islegal



reset
final public CharsetEncoder reset()(Code)
Reset this encoder. This method will reset internal status, and then call implReset() to reset any status related to specific charset. this encoder



unmappableCharacterAction
public CodingErrorAction unmappableCharacterAction()(Code)
Gets this encoder's CodingErrorAction when unmappable character occurred during encoding process. this encoder's CodingErrorAction when unmappablecharacter occurred during encoding process.



Methods inherited from java.lang.Object
protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object object)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final public Class<? extends Object> getClass()(Code)(Java Doc)
public int hashCode()(Code)(Java Doc)
final public void notify()(Code)(Java Doc)
final public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final public void wait(long millis, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait(long millis) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.