38 |
|
|
39 |
package gnu.java.io.encode; |
package gnu.java.io.encode; |
40 |
|
|
|
import java.io.CharConversionException; |
|
41 |
import java.io.IOException; |
import java.io.IOException; |
42 |
import java.io.OutputStream; |
import java.io.OutputStream; |
43 |
import java.io.Writer; |
import java.io.Writer; |
75 |
*/ |
*/ |
76 |
protected final OutputStream out; |
protected final OutputStream out; |
77 |
|
|
|
/** |
|
|
* This is the value that is substituted for bad characters that can't |
|
|
* be encoded. |
|
|
*/ |
|
|
protected char bad_char; |
|
|
|
|
|
/** |
|
|
* This indicates whether or not the bad char is set or not |
|
|
*/ |
|
|
protected boolean bad_char_set; |
|
|
|
|
78 |
/*************************************************************************/ |
/*************************************************************************/ |
79 |
|
|
80 |
/* |
/* |
131 |
* |
* |
132 |
* @return The number of bytes than will be encoded from the char array |
* @return The number of bytes than will be encoded from the char array |
133 |
* |
* |
|
* @exception CharConversionException If bad char value are encountered for this encoding |
|
134 |
*/ |
*/ |
135 |
public int |
public int |
136 |
bytesInCharArray(char[] buf) throws CharConversionException |
bytesInCharArray(char[] buf) |
137 |
{ |
{ |
138 |
return(bytesInCharArray(buf, 0, buf.length)); |
return(bytesInCharArray(buf, 0, buf.length)); |
139 |
} |
} |
156 |
* |
* |
157 |
* @return The number of bytes than can be encoded from the char array |
* @return The number of bytes than can be encoded from the char array |
158 |
* |
* |
|
* @exception CharConversionException If bad char value are encountered for this encoding |
|
159 |
*/ |
*/ |
160 |
public abstract int |
public abstract int |
161 |
bytesInCharArray(char[] buf, int offset, int len) throws CharConversionException; |
bytesInCharArray(char[] buf, int offset, int len); |
162 |
|
|
163 |
/*************************************************************************/ |
/*************************************************************************/ |
164 |
|
|
170 |
* |
* |
171 |
* @return The converted bytes array |
* @return The converted bytes array |
172 |
* |
* |
|
* @exception CharConversionException If an error occurs |
|
173 |
*/ |
*/ |
174 |
public byte[] |
public byte[] |
175 |
convertToBytes(char[] buf) throws CharConversionException |
convertToBytes(char[] buf) |
176 |
{ |
{ |
177 |
return(convertToBytes(buf, 0, buf.length)); |
return(convertToBytes(buf, 0, buf.length)); |
178 |
} |
} |
190 |
* |
* |
191 |
* @return The converted byte array |
* @return The converted byte array |
192 |
* |
* |
|
* @exception CharConversionException If an error occurs. |
|
193 |
*/ |
*/ |
194 |
public byte[] |
public byte[] |
195 |
convertToBytes(char[] buf, int offset, int len) throws CharConversionException |
convertToBytes(char[] buf, int offset, int len) |
196 |
{ |
{ |
197 |
byte[] bbuf = new byte[bytesInCharArray(buf, offset, len)]; |
byte[] bbuf = new byte[bytesInCharArray(buf, offset, len)]; |
198 |
|
|
217 |
* @return The byte array passed by the caller as a param, now filled with converted bytes. |
* @return The byte array passed by the caller as a param, now filled with converted bytes. |
218 |
* |
* |
219 |
* @exception ArrayIndexOutOfBoundsException If the destination byte array is not big enough to hold all the converted bytes |
* @exception ArrayIndexOutOfBoundsException If the destination byte array is not big enough to hold all the converted bytes |
|
* @exception CharConversionException If any other error occurs. |
|
220 |
*/ |
*/ |
221 |
public byte[] |
public byte[] |
222 |
convertToBytes(char[] buf, byte[] bbuf, int bbuf_offset) throws |
convertToBytes(char[] buf, byte[] bbuf, int bbuf_offset) |
|
CharConversionException |
|
223 |
{ |
{ |
224 |
return(convertToBytes(buf, 0, buf.length, bbuf, bbuf_offset)); |
return(convertToBytes(buf, 0, buf.length, bbuf, bbuf_offset)); |
225 |
} |
} |
245 |
* @return The byte array passed by the caller as a param, now filled with converted bytes. |
* @return The byte array passed by the caller as a param, now filled with converted bytes. |
246 |
* |
* |
247 |
* @exception ArrayIndexOutOfBoundsException If the destination byte array is not big enough to hold all the converted bytes. |
* @exception ArrayIndexOutOfBoundsException If the destination byte array is not big enough to hold all the converted bytes. |
|
* @exception CharConversionException If any other error occurs. |
|
248 |
*/ |
*/ |
249 |
public abstract byte[] |
public abstract byte[] |
250 |
convertToBytes(char[] buf, int buf_offset, int len, byte[] bbuf, |
convertToBytes(char[] buf, int buf_offset, int len, byte[] bbuf, |
251 |
int bbuf_offset) throws CharConversionException; |
int bbuf_offset); |
252 |
|
|
253 |
/*************************************************************************/ |
/*************************************************************************/ |
254 |
|
|
278 |
|
|
279 |
/*************************************************************************/ |
/*************************************************************************/ |
280 |
|
|
|
/** |
|
|
* This method sets that character that will be used when converting |
|
|
* a Unicode character that is invalid in the current encoding. If this |
|
|
* is set, it will be substituted for the bad value. Otherwise, an |
|
|
* exception will be thrown. Note that this character itself must be |
|
|
* valid for the current encoding. If it is not, an exception is thrown. |
|
|
* |
|
|
* @param bad_char The substitute for any bad characters found. |
|
|
* |
|
|
* @exception CharConversionException If the bad char value is not valid in this encoding |
|
|
*/ |
|
|
public void |
|
|
setBadCharValue(char bad_char) throws CharConversionException |
|
|
{ |
|
|
char[] buf = new char[1]; |
|
|
buf[0] = bad_char; |
|
|
convertToBytes(buf); |
|
|
|
|
|
this.bad_char = bad_char; |
|
|
bad_char_set = true; |
|
|
} |
|
|
|
|
281 |
} // class Encoder |
} // class Encoder |
282 |
|
|