Search in sources :

Example 61 with Charset

use of java.nio.charset.Charset in project jdk8u_jdk by JetBrains.

the class StreamTest method testLines.

// Test borrowed from BytesAndLines
public void testLines() throws IOException {
    final Charset US_ASCII = Charset.forName("US-ASCII");
    Path tmpfile = Files.createTempFile("blah", "txt");
    try {
        // zero lines
        assertTrue(Files.size(tmpfile) == 0, "File should be empty");
        try (Stream<String> s = Files.lines(tmpfile)) {
            checkLines(s, Collections.emptyList());
        }
        try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
            checkLines(s, Collections.emptyList());
        }
        // one line
        List<String> oneLine = Arrays.asList("hi");
        Files.write(tmpfile, oneLine, US_ASCII);
        try (Stream<String> s = Files.lines(tmpfile)) {
            checkLines(s, oneLine);
        }
        try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
            checkLines(s, oneLine);
        }
        // two lines using platform's line separator
        List<String> twoLines = Arrays.asList("hi", "there");
        Files.write(tmpfile, twoLines, US_ASCII);
        try (Stream<String> s = Files.lines(tmpfile)) {
            checkLines(s, twoLines);
        }
        try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
            checkLines(s, twoLines);
        }
        // MalformedInputException
        byte[] bad = { (byte) 0xff, (byte) 0xff };
        Files.write(tmpfile, bad);
        try (Stream<String> s = Files.lines(tmpfile)) {
            checkMalformedInputException(s);
        }
        try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
            checkMalformedInputException(s);
        }
        // NullPointerException
        checkNullPointerException(() -> Files.lines(null));
        checkNullPointerException(() -> Files.lines(null, US_ASCII));
        checkNullPointerException(() -> Files.lines(tmpfile, null));
    } finally {
        Files.delete(tmpfile);
    }
}
Also used : Path(java.nio.file.Path) Charset(java.nio.charset.Charset)

Example 62 with Charset

use of java.nio.charset.Charset in project jdk8u_jdk by JetBrains.

the class BytesAndLines method testNulls.

/**
     * Exercise NullPointerException
     */
public void testNulls() {
    Path file = Paths.get("foo");
    byte[] bytes = new byte[100];
    List<String> lines = Collections.emptyList();
    checkNullPointerException(() -> Files.readAllBytes(null));
    checkNullPointerException(() -> Files.write(null, bytes));
    checkNullPointerException(() -> Files.write(file, (byte[]) null));
    checkNullPointerException(() -> Files.write(file, bytes, (OpenOption[]) null));
    checkNullPointerException(() -> Files.write(file, bytes, new OpenOption[] { null }));
    checkNullPointerException(() -> Files.readAllLines(null));
    checkNullPointerException(() -> Files.readAllLines(file, (Charset) null));
    checkNullPointerException(() -> Files.readAllLines(null, Charset.defaultCharset()));
    checkNullPointerException(() -> Files.write(null, lines));
    checkNullPointerException(() -> Files.write(file, (List<String>) null));
    checkNullPointerException(() -> Files.write(file, lines, (OpenOption[]) null));
    checkNullPointerException(() -> Files.write(file, lines, new OpenOption[] { null }));
    checkNullPointerException(() -> Files.write(null, lines, Charset.defaultCharset()));
    checkNullPointerException(() -> Files.write(file, null, Charset.defaultCharset()));
    checkNullPointerException(() -> Files.write(file, lines, (Charset) null));
    checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), (OpenOption[]) null));
    checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), new OpenOption[] { null }));
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) Charset(java.nio.charset.Charset) ArrayList(java.util.ArrayList) List(java.util.List)

Example 63 with Charset

use of java.nio.charset.Charset in project weex-example by KalicyZhou.

the class PDF417Writer method encode.

@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (format != BarcodeFormat.PDF_417) {
        throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
    }
    PDF417 encoder = new PDF417();
    int margin = WHITE_SPACE;
    int errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL;
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
            encoder.setCompact(Boolean.valueOf(hints.get(EncodeHintType.PDF417_COMPACT).toString()));
        }
        if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
            encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString()));
        }
        if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
            Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
            encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows());
        }
        if (hints.containsKey(EncodeHintType.MARGIN)) {
            margin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
        }
        if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
            errorCorrectionLevel = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
        }
        if (hints.containsKey(EncodeHintType.CHARACTER_SET)) {
            Charset encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
            encoder.setEncoding(encoding);
        }
    }
    return bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin);
}
Also used : Dimensions(com.google.zxing.pdf417.encoder.Dimensions) Charset(java.nio.charset.Charset) PDF417(com.google.zxing.pdf417.encoder.PDF417)

Example 64 with Charset

use of java.nio.charset.Charset in project weex-example by KalicyZhou.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(int[] codewords, String ecLevel) throws FormatException {
    StringBuilder result = new StringBuilder(codewords.length * 2);
    Charset encoding = DEFAULT_ENCODING;
    // Get compaction mode
    int codeIndex = 1;
    int code = codewords[codeIndex++];
    PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata();
    while (codeIndex < codewords[0]) {
        switch(code) {
            case TEXT_COMPACTION_MODE_LATCH:
                codeIndex = textCompaction(codewords, codeIndex, result);
                break;
            case BYTE_COMPACTION_MODE_LATCH:
            case BYTE_COMPACTION_MODE_LATCH_6:
                codeIndex = byteCompaction(code, codewords, encoding, codeIndex, result);
                break;
            case MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
                result.append((char) codewords[codeIndex++]);
                break;
            case NUMERIC_COMPACTION_MODE_LATCH:
                codeIndex = numericCompaction(codewords, codeIndex, result);
                break;
            case ECI_CHARSET:
                CharacterSetECI charsetECI = CharacterSetECI.getCharacterSetECIByValue(codewords[codeIndex++]);
                encoding = Charset.forName(charsetECI.name());
                break;
            case ECI_GENERAL_PURPOSE:
                // Can't do anything with generic ECI; skip its 2 characters
                codeIndex += 2;
                break;
            case ECI_USER_DEFINED:
                // Can't do anything with user ECI; skip its 1 character
                codeIndex++;
                break;
            case BEGIN_MACRO_PDF417_CONTROL_BLOCK:
                codeIndex = decodeMacroBlock(codewords, codeIndex, resultMetadata);
                break;
            case BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
            case MACRO_PDF417_TERMINATOR:
                // Should not see these outside a macro block
                throw FormatException.getFormatInstance();
            default:
                // Default to text compaction. During testing numerous barcodes
                // appeared to be missing the starting mode. In these cases defaulting
                // to text compaction seems to work.
                codeIndex--;
                codeIndex = textCompaction(codewords, codeIndex, result);
                break;
        }
        if (codeIndex < codewords.length) {
            code = codewords[codeIndex++];
        } else {
            throw FormatException.getFormatInstance();
        }
    }
    if (result.length() == 0) {
        throw FormatException.getFormatInstance();
    }
    DecoderResult decoderResult = new DecoderResult(null, result.toString(), null, ecLevel);
    decoderResult.setOther(resultMetadata);
    return decoderResult;
}
Also used : Charset(java.nio.charset.Charset) DecoderResult(com.google.zxing.common.DecoderResult) PDF417ResultMetadata(com.google.zxing.pdf417.PDF417ResultMetadata) CharacterSetECI(com.google.zxing.common.CharacterSetECI)

Example 65 with Charset

use of java.nio.charset.Charset in project jdk8u_jdk by JetBrains.

the class Test6254467 method main.

public static void main(String[] args) throws Exception {
    Charset ebcdic = Charset.forName("ebcdic-cp-us");
    ebcdic = Charset.forName("ebcdic-cp-ca");
    ebcdic = Charset.forName("ebcdic-cp-wt");
    ebcdic = Charset.forName("ebcdic-cp-nl");
}
Also used : Charset(java.nio.charset.Charset)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48