fhueske commented on a change in pull request #6823: [FLINK-10134] UTF-16 support for TextInputFormat bug refixed URL: https://github.com/apache/flink/pull/6823#discussion_r225547565
########## File path: flink-core/src/main/java/org/apache/flink/api/common/io/DelimitedInputFormat.java ########## @@ -504,16 +533,119 @@ private void initBuffers() { this.end = false; } + /** + * Set file bom encoding. + * + * @param split + */ + private void setBomFileCharset(FileInputSplit split) { + try { + String filePath = split.getPath().toString(); + if (this.fileBomCharsetMap.containsKey(filePath)) { + this.bomCharset = this.fileBomCharsetMap.get(filePath); + } else { + byte[] checkBytes = new byte[]{(byte) 0x00, (byte) 0xFE, (byte) 0xFF, (byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; + byte[] bomBuffer = new byte[4]; + + if (this.splitStart != 0) { + this.stream.seek(0); + this.stream.read(bomBuffer, 0, bomBuffer.length); + this.stream.seek(split.getStart()); + } else { + System.arraycopy(this.readBuffer, 0, bomBuffer, 0, 3); + } + + if ((bomBuffer[0] == checkBytes[0]) && (bomBuffer[1] == checkBytes[0]) && (bomBuffer[2] == checkBytes[1]) + && (bomBuffer[3] == checkBytes[2])) { + this.bomCharset = Charset.forName("UTF-32BE"); + } else if ((bomBuffer[0] == checkBytes[2]) && (bomBuffer[1] == checkBytes[1]) && (bomBuffer[2] == checkBytes[0]) + && (bomBuffer[3] == checkBytes[0])) { + this.bomCharset = Charset.forName("UTF-32LE"); + } else if ((bomBuffer[0] == checkBytes[3]) && (bomBuffer[1] == checkBytes[4]) && (bomBuffer[2] == checkBytes[5])) { + this.bomCharset = Charset.forName("UTF-8"); + } else if ((bomBuffer[0] == checkBytes[1]) && (bomBuffer[1] == checkBytes[2])) { + this.bomCharset = Charset.forName("UTF-16BE"); + } else if ((bomBuffer[0] == checkBytes[2]) && (bomBuffer[1] == checkBytes[1])) { + this.bomCharset = Charset.forName("UTF-16LE"); + } else { + this.bomCharset = Charset.forName(charsetName); + } + + this.fileBomCharsetMap.put(filePath, this.bomCharset); + } + } catch (Exception e) { + LOG.warn("Failed to get file bom encoding."); + this.bomCharset = Charset.forName(charsetName); + } + + setParasByCharset(this.bomCharset); + } + + /** + * Set stepSize, delimiterNewLinePos, delimiterCarrageReturnPos by charset. + * + * @param charset + */ + private void setParasByCharset(Charset charset) { Review comment: This is very special logic for the default delimiter. We need to get the correct `delimiter` bytes also for custom delimiter strings. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services