The input $itype$ sequence is provided in a $itype$ buffer or a series of such buffers. The output $otype$ sequence is written to a $otype$ buffer or a series of such buffers. $A$ $coder$ should always be used by making the following sequence of method invocations, hereinafter referred to as $a$ $coding$ operation:
Reset the $coder$ via the {@link #reset reset} method, unless ithas not been used before;
Invoke the {@link #$code$ $code$} method zero or more times, aslong as additional input may be available, passing false for the endOfInput argument and filling the input buffer and flushing the output buffer between invocations;
Invoke the {@link #$code$ $code$} method one final time, passingtrue for the endOfInput argument; and then
Invoke the {@link #flush flush} method so that the $coder$ canflush any internal state to the output buffer.
There are two general types of $coding$ errors. If the input $itype$ sequence is $notLegal$ then the input is considered malformed. If the input $itype$ sequence is legal but cannot be mapped to a valid $outSequence$ then an unmappable character has been encountered.
Instances of this class are not safe for use by multiple concurrent threads.
@author Mark Reinhold
@author JSR-51 Expert Group
@since 1.4
@see ByteBuffer
@see CharBuffer
@see Charset
@see Charset$OtherCoder$
* Decode the string encoded in the bytebuffer in UTF-8 format.
* @param buffer the given buffer to analyze.
* @return the decoded string
*/
protected String decode(final ByteBuffer buffer) {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder charsetDecoder = charset.newDecoder();
CharBuffer charBuffer = null;
try {
charBuffer = charsetDecoder.decode(buffer);
} catch (CharacterCodingException e) {
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
|| csn.equals(sd.charsetName()))) {
sd = null;
try {
Charset cs = lookupCharset(csn);
if (cs != null)
sd = new StringDecoder(cs, csn);
} catch (IllegalCharsetNameException x) {}
if (sd == null)
throw new UnsupportedEncodingException(csn);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((se == null) || !(csn.equals(se.requestedCharsetName())
|| csn.equals(se.charsetName()))) {
se = null;
try {
Charset cs = lookupCharset(csn);
if (cs != null)
se = new StringEncoder(cs, csn);
} catch (IllegalCharsetNameException x) {}
if (se == null)
throw new UnsupportedEncodingException (csn);
public static String encode(String s, String enc)
throws UnsupportedEncodingException {
boolean needToChange = false;
StringBuffer out = new StringBuffer(s.length());
Charset charset;
CharArrayWriter charArrayWriter = new CharArrayWriter();
if (enc == null)
throw new NullPointerException("charsetName");
}
return text;
}
protected String readTextFromStream(InputStream aTextStream) {
Charset charset = getCharSet();
int size = urlConnection.getContentLength();
ByteArrayOutputStream bos = new ByteArrayOutputStream(size != -1 ? size : 1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = aTextStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
String streamedText = ""; // be robust
try {
streamedText = bos.toString(charset.name());
} catch (UnsupportedEncodingException e) {} // we already checked for that above
// check Content-Type text/plain; charset=iso-8859-1
return streamedText;
}
*/
protected Charset getCharSet() {
String contentType = urlConnection.getContentType();
// find out about the charset from the URLConnection
Matcher m = charsetPattern.matcher(contentType);
Charset charset = Charset.forName("iso-8859-1"); // default charset
if (m.find()) {
String charsetString = m.group(1);
try {
charset = Charset.forName(charsetString);
} catch (IllegalCharsetNameException e) {
*/
public static String toString(ByteBuffer buffer, String encoding) throws UnsupportedEncodingException {
try {
CharsetDecoder decoder = decoders.get(encoding);
if (decoder == null) {
Charset charset = Charset.forName(encoding);
decoder = charset.newDecoder();
decoders.put(encoding, decoder);
encoders.put(encoding, charset.newEncoder());
}
return decoder.decode(buffer).toString();
} catch (CharacterCodingException cce) {
public static ByteBuffer getAsByteBuffer() {
try {
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
return buf;
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
public static ByteBuffer getAsByteBuffer() {
try {
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
return buf;
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
public static ByteBuffer getAsByteBuffer() {
try {
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
return buf;
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
Related Classes of java.nio.charset.Charset$Coder$
Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.