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/CharsetToBytesAdaptor.java | |
download | cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.tar.bz2 cbb-gcc-4.6.4-15d2061ac0796199866debe9ac87130894b0cdd3.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/CharsetToBytesAdaptor.java')
-rw-r--r-- | libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java b/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java new file mode 100644 index 000000000..80e749cc8 --- /dev/null +++ b/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java @@ -0,0 +1,156 @@ +/* Copyright (C) 2005, 2006 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.CharsetEncoder; +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 UnicodeToBytes converter. + */ +public class CharsetToBytesAdaptor extends UnicodeToBytes +{ + /** + * The CharsetEncoder that does all the work. + */ + private final CharsetEncoder encoder; + + /** + * ByteBuffer wrapper for this.buf. + */ + private ByteBuffer outBuf; + + /** + * True if we've told the CharsetEncoder that there are no more + * characters available. + */ + private boolean closedEncoder; + + /** + * True if there are bytes pending in the encoder. + */ + private boolean hasBytes; + + /** + * True if we're finished. + */ + private boolean finished; + + /** + * Create a new CharsetToBytesAdaptor for the given Charset. + * + * @param cs The Charset. + */ + public CharsetToBytesAdaptor(Charset cs) + { + this(cs.newEncoder()); + } + + /** + * Create a new CharsetToBytesAdaptor for the given CharsetEncoder. + * + * @param enc The CharsetEncoder. + */ + public CharsetToBytesAdaptor(CharsetEncoder enc) + { + encoder = enc; + // Use default replacments on bad input so that we don't have to + // deal with errors. + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } + + /** + * Return the encoder's name. The backing Charset's name is + * returned. + * + * @return The name. + */ + public String getName() + { + return EncodingHelper.getOldCanonical(encoder.charset().name()); + } + + public int write (char[] inbuffer, int inpos, int inlength) + { + // Wrap the char array so it can be used by the encoder. + CharBuffer b = CharBuffer.wrap(inbuffer, inpos, inlength); + write(b); + return b.position() - inpos; // Number of chars consumed. + } + + public int write (String str, int inpos, int inlength, char work) + { + // Wrap the String so it can be used by the encoder. + CharBuffer b = CharBuffer.wrap(str, inpos, inlength); + write(b); + return b.position() - inpos; // Number of chars consumed. + } + + /** + * Encode as much of inBuf as will fit in buf. The number of + * chars consumed is reflected by the new position of inBuf. The + * output is put in buf and count is incremented by the number of + * bytes written. + * + * @param inBuf The input. + */ + private void write(CharBuffer inBuf) + { + // Reuse existing outBuf if it is still wrapping the same array + // it was created with. + if (outBuf == null || !outBuf.hasArray() || outBuf.array() != buf) + outBuf = ByteBuffer.wrap(buf); + + // Set the current position. + outBuf.position(count); + + // Do the conversion. + CoderResult result = encoder.encode(inBuf, outBuf, closedEncoder); + hasBytes = result == CoderResult.OVERFLOW; + if (closedEncoder) + { + result = encoder.flush(outBuf); + if (result == CoderResult.UNDERFLOW) + finished = true; + else + hasBytes = true; + } + + // Mark the new end of buf. + count = outBuf.position(); + } + + /** + * Check for cached output in the converter. + * + * @return true if there is cached output that has not been + * written to buf. + */ + public boolean havePendingBytes() + { + return hasBytes; + } + + public void setFinished() + { + closedEncoder = true; + } + + // These aren't cached. + public void done() + { + } +} |