diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java | |
download | cbb-gcc-4.6.4-upstream.tar.bz2 cbb-gcc-4.6.4-upstream.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java')
-rw-r--r-- | libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java b/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java new file mode 100644 index 000000000..4b43d7bee --- /dev/null +++ b/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java @@ -0,0 +1,90 @@ +/* Copyright (C) 2005, 2007 Free Software Foundation + +This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package gnu.gcj.convert; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.CoderResult; +import gnu.java.nio.charset.EncodingHelper; + +/** + * Adaptor class that allow any {@link Charset} to be used + * as a BytesToUnicode converter. + */ +public class BytesToCharsetAdaptor extends BytesToUnicode +{ + /** + * The CharsetDecoder that does all the work. + */ + private final CharsetDecoder decoder; + + /** + * ByteBuffer wrapper for this.buf. + */ + private ByteBuffer inBuf; + + /** + * Create a new BytesToCharsetAdaptor for the given Charset. + * + * @param cs the Charset. + */ + public BytesToCharsetAdaptor(Charset cs) + { + this(cs.newDecoder()); + } + + /** + * Create a new BytesToCharsetAdaptor for the given CharsetDecoder. + * + * @param dec the CharsetDecoder. + */ + public BytesToCharsetAdaptor(CharsetDecoder dec) + { + decoder = dec; + // Use default replacments on bad input so that we don't have to + // deal with errors. + decoder.onMalformedInput(CodingErrorAction.REPLACE); + decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } + + /** + * Return the decoder's name. The backing Charset's name is + * returned. + * + * @return The name. + */ + public String getName() + { + return EncodingHelper.getOldCanonical(decoder.charset().name()); + } + + public int read(char[] outbuffer, int outpos, int count) + { + if (inBuf == null || ! inBuf.hasArray() || inBuf.array() != inbuffer) + inBuf = ByteBuffer.wrap(inbuffer); + inBuf.limit(inlength); + inBuf.position(inpos); + + CharBuffer outBuf = CharBuffer.wrap(outbuffer, outpos, count); + decoder.decode(inBuf, outBuf, false); + + // Update this.inpos to reflect the bytes consumed. + inpos = inBuf.position(); + // Return the number of characters that were written to outbuffer. + return outBuf.position() - outpos; + } + + // These aren't cached. + public void done() + { + } +} |