use of org.apache.commons.io.ByteOrderMark in project applause by applause.
the class BOMInputStream method getBOM.
/**
* Return the BOM (Byte Order Mark).
*
* @return The BOM or null if none
* @throws IOException if an error reading the first bytes of the stream occurs
*/
public ByteOrderMark getBOM() throws IOException {
if (firstBytes == null) {
int max = 0;
for (ByteOrderMark bom : boms) {
max = Math.max(max, bom.length());
}
firstBytes = new int[max];
for (int i = 0; i < firstBytes.length; i++) {
firstBytes[i] = in.read();
fbLength++;
if (firstBytes[i] < 0) {
break;
}
byteOrderMark = find();
if (byteOrderMark != null) {
if (!include) {
fbLength = 0;
}
break;
}
}
}
return byteOrderMark;
}
use of org.apache.commons.io.ByteOrderMark in project disconf by knightliao.
the class MyStringUtils method multipartFileToString.
public static String multipartFileToString(MultipartFile file) throws IOException {
BOMInputStream bomInputStream = new BOMInputStream(file.getInputStream());
ByteOrderMark bom = bomInputStream.getBOM();
String charsetName = bom == null ? DEFAULT_ENCODING : bom.getCharsetName();
return inputStreamToString(bomInputStream, charsetName);
}
use of org.apache.commons.io.ByteOrderMark in project CFLint by cflint.
the class FileUtil method loadFile.
public static String loadFile(final File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
try {
final BOMInputStream bOMInputStream = new BOMInputStream(fis);
final ByteOrderMark bom = bOMInputStream.getBOM();
final String charsetName = bom == null ? DEFAULT_ENCODING : bom.getCharsetName();
InputStreamReader reader = new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName);
return readFully(reader);
} finally {
fis.close();
}
} catch (final Exception e) {
return null;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (final IOException e) {
}
}
}
use of org.apache.commons.io.ByteOrderMark in project sonarqube by SonarSource.
the class CharsetDetector method detectCharset.
private boolean detectCharset(byte[] buf) throws IOException {
ByteCharsetDetector detector = new ByteCharsetDetector(new CharsetValidation(), userEncoding);
ByteOrderMark bom = detector.detectBOM(buf);
if (bom != null) {
detectedCharset = Charset.forName(bom.getCharsetName());
stream.skip(bom.length());
return true;
}
detectedCharset = detector.detect(buf);
return detectedCharset != null;
}
use of org.apache.commons.io.ByteOrderMark in project AntennaPod by AntennaPod.
the class OpmlImportActivity method startImport.
/**
* Starts the import process.
*/
private void startImport() {
viewBinding.progressBar.setVisibility(View.VISIBLE);
Observable.fromCallable(() -> {
InputStream opmlFileStream = getContentResolver().openInputStream(uri);
BOMInputStream bomInputStream = new BOMInputStream(opmlFileStream);
ByteOrderMark bom = bomInputStream.getBOM();
String charsetName = (bom == null) ? "UTF-8" : bom.getCharsetName();
Reader reader = new InputStreamReader(bomInputStream, charsetName);
OpmlReader opmlReader = new OpmlReader();
ArrayList<OpmlElement> result = opmlReader.readDocument(reader);
reader.close();
return result;
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
viewBinding.progressBar.setVisibility(View.GONE);
Log.d(TAG, "Parsing was successful");
readElements = result;
listAdapter = new ArrayAdapter<>(OpmlImportActivity.this, android.R.layout.simple_list_item_multiple_choice, getTitleList());
viewBinding.feedlist.setAdapter(listAdapter);
}, e -> {
viewBinding.progressBar.setVisibility(View.GONE);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(R.string.error_label);
alert.setMessage(getString(R.string.opml_reader_error) + e.getMessage());
alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss());
alert.create().show();
});
}
Aggregations