FileIOUtils { public static Pair<Integer, Integer> getUtf8Range(final byte[] buffer, final int offset, final int length) { final int start = getUtf8ByteStart(buffer, offset); final int end = getUtf8ByteEnd(buffer, offset + length - 1); return new Pair<>(start, end - start + 1); }  static boolean isDirWritable(final File dir); static boolean isValidDirectory(File directory); static void deleteDirectorySilently(final File dir); static File getDirectory(File parentDirectory, String folderName); static void moveFiles(File sourceDirectory, File destinationDiretory, String fileNamePattern); static void dumpNumberToFile(final Path filePath, final long num); static long readNumberFromFile(final Path filePath); static String getSourcePathFromClass(final Class<?> containedClass); static String getCanonicalPath(final File f); static Props loadOutputFileProps(final File file); static File createOutputPropsFile(final String prefix,
      final String suffix, final String workingDir); static int createDeepHardlink(final File sourceDir, final File destDir); static Pair<Integer, Integer> readUtf8File(final File file, final int offset,
      final int length, final OutputStream stream); static LogData readUtf8File(final File file, final int fileOffset, final int length); static JobMetaData readUtf8MetaDataFile(final File file, final int fileOffset,
      final int length); static Pair<Integer, Integer> getUtf8Range(final byte[] buffer, final int offset,
      final int length);  }
@Test public void testAsciiUTF() throws IOException { final String foreignText = "abcdefghijklmnopqrstuvwxyz"; final byte[] utf8ByteArray = createUTF8ByteArray(foreignText); final int length = utf8ByteArray.length; System.out.println("char length:" + foreignText.length() + " utf8BytesLength:" + utf8ByteArray.length + " for:" + foreignText); final Pair<Integer, Integer> pair = FileIOUtils.getUtf8Range(utf8ByteArray, 1, length - 6); System.out.println("Pair :" + pair.toString()); final String recreatedString = new String(utf8ByteArray, 1, length - 6, "UTF-8"); System.out.println("recreatedString:" + recreatedString); final String correctString = new String(utf8ByteArray, pair.getFirst(), pair.getSecond(), "UTF-8"); System.out.println("correctString:" + correctString); assertEquals(pair, new Pair<>(1, 20)); assertEquals(correctString.length(), foreignText.length() - 6); }
@Test public void testForeignUTF() throws IOException { final String foreignText = "안녕하세요, 제 이름은 박병호입니다"; final byte[] utf8ByteArray = createUTF8ByteArray(foreignText); final int length = utf8ByteArray.length; System.out.println("char length:" + foreignText.length() + " utf8BytesLength:" + utf8ByteArray.length + " for:" + foreignText); final Pair<Integer, Integer> pair = FileIOUtils.getUtf8Range(utf8ByteArray, 1, length - 6); System.out.println("Pair :" + pair.toString()); final String recreatedString = new String(utf8ByteArray, 1, length - 6, "UTF-8"); System.out.println("recreatedString:" + recreatedString); String correctString = new String(utf8ByteArray, pair.getFirst(), pair.getSecond(), "UTF-8"); System.out.println("correctString:" + correctString); assertEquals(pair, new Pair<>(3, 40)); assertEquals(correctString.length(), foreignText.length() - 3); final String mixedText = "abc안녕하세요, 제 이름은 박병호입니다"; final byte[] mixedBytes = createUTF8ByteArray(mixedText); final Pair<Integer, Integer> pair2 = FileIOUtils.getUtf8Range(mixedBytes, 1, length - 4); correctString = new String(mixedBytes, pair2.getFirst(), pair2.getSecond(), "UTF-8"); System.out.println("correctString:" + correctString); assertEquals(pair2, new Pair<>(1, 45)); assertEquals(correctString.length(), mixedText.length() - 3); }