FilterUtf8Mb4Util { public static String filterOffUtf8Mb4(String text) { byte[] bytes = text.getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); int i = 0; while (i < bytes.length) { short b = bytes[i]; if (b > 0) { buffer.put(bytes[i++]); continue; } b += 256; if (((b >> 5) ^ 0x06) == 0) { buffer.put(bytes, i, 2); i += 2; } else if (((b >> 4) ^ 0x0E) == 0) { buffer.put(bytes, i, 3); i += 3; } else if (((b >> 3) ^ 0x1E) == 0) { i += 4; } else if (((b >> 2) ^ 0xBE) == 0) { i += 5; } else { i += 6; } } buffer.flip(); return new String(buffer.array(), StandardCharsets.UTF_8); } private  FilterUtf8Mb4Util(); static String filterOffUtf8Mb4(String text); static boolean containsOutOfUtf8(String source);  }
@Test public void filterOffUtf8Mb4() throws Exception { String illegalStr1 = "An 😀awesome 😃string with a few 😉emojis!"; String illegalStr2 = "\uD83C\uDFFB情シ"; assertTrue(!FilterUtf8Mb4Util.filterOffUtf8Mb4(illegalStr1).equals(illegalStr1)); assertTrue(!FilterUtf8Mb4Util.filterOffUtf8Mb4(illegalStr2).equals(illegalStr2)); }