Java Code Examples for java.nio.charset.CharacterCodingException#printStackTrace()

The following examples show how to use java.nio.charset.CharacterCodingException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KinesisRecordToTupleMapper.java    From streamline with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: FilenameUtils.java    From MLib with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: Text.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 4
Source File: Text.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 5
Source File: HTTPWriteHandler.java    From AdditionsAPI with MIT License 5 votes vote down vote up
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();
	}
}
 
Example 6
Source File: Text.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 7
Source File: FilenameUtils.java    From MLib with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: Text.java    From Canova with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 9
Source File: Text.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 10
Source File: StringRecord.java    From stratosphere with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
	}
}
 
Example 11
Source File: Text.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}