1 /* Copyright (C) 2005, 2006 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package gnu
.gcj
.convert
;
11 import java
.nio
.ByteBuffer
;
12 import java
.nio
.CharBuffer
;
13 import java
.nio
.charset
.Charset
;
14 import java
.nio
.charset
.CharsetEncoder
;
15 import java
.nio
.charset
.CodingErrorAction
;
16 import java
.nio
.charset
.CoderResult
;
17 import gnu
.java
.nio
.charset
.EncodingHelper
;
20 * Adaptor class that allow any {@link Charset} to be used
21 * as a UnicodeToBytes converter.
23 public class CharsetToBytesAdaptor
extends UnicodeToBytes
26 * The CharsetEncoder that does all the work.
28 private final CharsetEncoder encoder
;
31 * ByteBuffer wrapper for this.buf.
33 private ByteBuffer outBuf
;
36 * True if we've told the CharsetEncoder that there are no more
37 * characters available.
39 private boolean closedEncoder
;
42 * True if we're finished.
44 private boolean finished
;
47 * Create a new CharsetToBytesAdaptor for the given Charset.
49 * @param cs The Charset.
51 public CharsetToBytesAdaptor(Charset cs
)
53 this(cs
.newEncoder());
57 * Create a new CharsetToBytesAdaptor for the given CharsetEncoder.
59 * @param enc The CharsetEncoder.
61 public CharsetToBytesAdaptor(CharsetEncoder enc
)
64 // Use default replacments on bad input so that we don't have to
66 encoder
.onMalformedInput(CodingErrorAction
.REPLACE
);
67 encoder
.onUnmappableCharacter(CodingErrorAction
.REPLACE
);
71 * Return the encoder's name. The backing Charset's name is
76 public String
getName()
78 return EncodingHelper
.getOldCanonical(encoder
.charset().name());
81 public int write (char[] inbuffer
, int inpos
, int inlength
)
83 // Wrap the char array so it can be used by the encoder.
84 CharBuffer b
= CharBuffer
.wrap(inbuffer
, inpos
, inlength
);
86 return b
.position() - inpos
; // Number of chars consumed.
89 public int write (String str
, int inpos
, int inlength
, char work
)
91 // Wrap the String so it can be used by the encoder.
92 CharBuffer b
= CharBuffer
.wrap(str
, inpos
, inlength
);
94 return b
.position() - inpos
; // Number of chars consumed.
98 * Encode as much of inBuf as will fit in buf. The number of
99 * chars consumed is reflected by the new position of inBuf. The
100 * output is put in buf and count is incremented by the number of
103 * @param inBuf The input.
105 private void write(CharBuffer inBuf
)
107 // Reuse existing outBuf if it is still wrapping the same array
108 // it was created with.
109 if (outBuf
== null || !outBuf
.hasArray() || outBuf
.array() != buf
)
110 outBuf
= ByteBuffer
.wrap(buf
);
112 // Set the current position.
113 outBuf
.position(count
);
115 // If we've already said that there is no more input available,
116 // then we simply try to flush again.
119 CoderResult result
= encoder
.flush(outBuf
);
120 if (result
== CoderResult
.UNDERFLOW
)
125 // Do the conversion. If there are no characters to write,
126 // then we are finished.
127 closedEncoder
= ! inBuf
.hasRemaining();
128 encoder
.encode(inBuf
, outBuf
, closedEncoder
);
131 // Mark the new end of buf.
132 count
= outBuf
.position();
136 * Check for cached output in the converter.
138 * @return true if there is cached output that has not been
141 public boolean havePendingBytes()
146 // These aren't cached.