下面列出了java.nio.charset.CharacterCodingException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public List<Object> getTuple(Record record) {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
List<Object> tuple = new ArrayList<>();
tuple.add(record.getPartitionKey());
tuple.add(record.getSequenceNumber());
try {
String data = decoder.decode(record.getData()).toString();
tuple.add(data);
} catch (CharacterCodingException e) {
e.printStackTrace();
LOG.warn("Exception occured. Emitting tuple with empty string data", e);
tuple.add("");
}
return tuple;
}
/**
* Convert a filename from Java´s native UTF-16 to OS native character encoding.
*
* @param fileName The UTF-16 filename string.
* @return Natively encoded string for the OS.
*/
private static String convertToNativeEncoding(String fileName, boolean isPath) {
String ret = fileName;
ret = removeIllegalCharacters(ret, isPath);
//convert our filename to OS encoding...
try {
final CharsetEncoder charsetEncoder = Charset.defaultCharset().newEncoder();
charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE); // otherwise breaks on first unconvertable char
charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
charsetEncoder.replaceWith(new byte[]{'_'});
final ByteBuffer buf = charsetEncoder.encode(CharBuffer.wrap(ret));
if (buf.hasArray()) {
ret = new String(buf.array());
}
//remove NUL character from conversion...
ret = ret.replaceAll("\\u0000", "");
} catch (CharacterCodingException e) {
e.printStackTrace();
}
return ret;
}
/**
* Finds any occurence of <code>what</code> in the backing buffer, starting as position <code>start</code>. The
* starting position is measured in bytes and the return value is in terms of byte position in the buffer. The backing
* buffer is not converted to a string for this operation.
*
* @return byte position of the first occurence of the search string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position() - 1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) {
return pos;
}
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
/**
* Finds any occurence of <code>what</code> in the backing
* buffer, starting as position <code>start</code>. The starting
* position is measured in bytes and the return value is in
* terms of byte position in the buffer. The backing buffer is
* not converted to a string for this operation.
* @return byte position of the first occurence of the search
* string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes,0,this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position()-1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) return pos;
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
private void buildBuffer(HTTPResponse response)
{
String headerString = response.getHeaderString();
CharBuffer charBuffer = CharBuffer.wrap(headerString.toCharArray());
try
{
response.buffer = server.encoder.encode(charBuffer);
}
catch (CharacterCodingException e)
{
//should not happen
e.printStackTrace();
}
}
/**
* Finds any occurence of <code>what</code> in the backing
* buffer, starting as position <code>start</code>. The starting
* position is measured in bytes and the return value is in
* terms of byte position in the buffer. The backing buffer is
* not converted to a string for this operation.
* @return byte position of the first occurence of the search
* string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes,0,this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position()-1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) return pos;
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
/**
* Convert a filename from Java´s native UTF-16 to US-ASCII character encoding.
*
* @param fileName The UTF-16 filename string.
* @return US-ASCII encoded string for the OS.
*/
private static String convertToASCIIEncoding(String fileName, boolean isPath) {
String ret = fileName;
ret = ret.replace("ä", "ae");
ret = ret.replace("ö", "oe");
ret = ret.replace("ü", "ue");
ret = ret.replace("Ä", "Ae");
ret = ret.replace("Ö", "Oe");
ret = ret.replace("Ü", "Ue");
ret = ret.replace("ß", "ss");
// ein Versuch zu vereinfachen
ret = cleanUnicode(ret);
ret = removeIllegalCharacters(ret, isPath);
//convert our filename to OS encoding...
try {
final CharsetEncoder charsetEncoder = Charset.forName("US-ASCII").newEncoder();
charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE); // otherwise breaks on first unconvertable char
charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
charsetEncoder.replaceWith(new byte[]{'_'});
final ByteBuffer buf = charsetEncoder.encode(CharBuffer.wrap(ret));
if (buf.hasArray()) {
ret = new String(buf.array());
}
//remove NUL character from conversion...
ret = ret.replaceAll("\\u0000", "");
} catch (CharacterCodingException e) {
e.printStackTrace();
}
return ret;
}
/**
* Finds any occurence of <code>what</code> in the backing
* buffer, starting as position <code>start</code>. The starting
* position is measured in bytes and the return value is in
* terms of byte position in the buffer. The backing buffer is
* not converted to a string for this operation.
* @return byte position of the first occurence of the search
* string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes,0,this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position()-1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) return pos;
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
/**
* Finds any occurence of <code>what</code> in the backing
* buffer, starting as position <code>start</code>. The starting
* position is measured in bytes and the return value is in
* terms of byte position in the buffer. The backing buffer is
* not converted to a string for this operation.
* @return byte position of the first occurence of the search
* string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes,0,this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position()-1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) return pos;
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
/**
* Finds any occurrence of <code>what</code> in the backing buffer, starting
* as position <code>start</code>. The starting position is measured in
* bytes and the return value is in terms of byte position in the buffer.
* The backing buffer is not converted to a string for this operation.
*
* @return byte position of the first occurence of the search string in the
* UTF-8 buffer or -1 if not found
*/
public int find(final String what, final int start) {
try {
final ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length);
final ByteBuffer tgt = encode(what);
final byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
final int pos = src.position() - 1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) {
return pos;
}
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}
/**
* Finds any occurence of <code>what</code> in the backing
* buffer, starting as position <code>start</code>. The starting
* position is measured in bytes and the return value is in
* terms of byte position in the buffer. The backing buffer is
* not converted to a string for this operation.
* @return byte position of the first occurence of the search
* string in the UTF-8 buffer or -1 if not found
*/
public int find(String what, int start) {
try {
ByteBuffer src = ByteBuffer.wrap(this.bytes,0,this.length);
ByteBuffer tgt = encode(what);
byte b = tgt.get();
src.position(start);
while (src.hasRemaining()) {
if (b == src.get()) { // matching first byte
src.mark(); // save position in loop
tgt.mark(); // save position in target
boolean found = true;
int pos = src.position()-1;
while (tgt.hasRemaining()) {
if (!src.hasRemaining()) { // src expired first
tgt.reset();
src.reset();
found = false;
break;
}
if (!(tgt.get() == src.get())) {
tgt.reset();
src.reset();
found = false;
break; // no match
}
}
if (found) return pos;
}
}
return -1; // not found
} catch (CharacterCodingException e) {
// can't get here
e.printStackTrace();
return -1;
}
}