Search in sources :

Example 1 with ByteOrderMark

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;
}
Also used : ByteOrderMark(org.apache.commons.io.ByteOrderMark)

Example 2 with 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);
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) ByteOrderMark(org.apache.commons.io.ByteOrderMark)

Example 3 with ByteOrderMark

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) {
        }
    }
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) ByteOrderMark(org.apache.commons.io.ByteOrderMark) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 4 with ByteOrderMark

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;
}
Also used : ByteOrderMark(org.apache.commons.io.ByteOrderMark)

Example 5 with ByteOrderMark

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();
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) BOMInputStream(org.apache.commons.io.input.BOMInputStream) ByteOrderMark(org.apache.commons.io.ByteOrderMark) InputStreamReader(java.io.InputStreamReader) BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStream(java.io.InputStream) OpmlReader(de.danoeh.antennapod.core.export.opml.OpmlReader) ArrayList(java.util.ArrayList) OpmlReader(de.danoeh.antennapod.core.export.opml.OpmlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Aggregations

ByteOrderMark (org.apache.commons.io.ByteOrderMark)5 BOMInputStream (org.apache.commons.io.input.BOMInputStream)3 InputStreamReader (java.io.InputStreamReader)2 AlertDialog (androidx.appcompat.app.AlertDialog)1 OpmlReader (de.danoeh.antennapod.core.export.opml.OpmlReader)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1