Search in sources :

Example 16 with PushbackInputStream

use of java.io.PushbackInputStream in project robovm by robovm.

the class OldPushbackInputStreamTest method test_unread$B.

public void test_unread$B() throws IOException {
    PushbackInputStream tobj;
    String str2 = "0123456789";
    byte[] buf2 = str2.getBytes();
    byte[] readBuf = new byte[10];
    tobj = new PushbackInputStream(underlying, 10);
    tobj.unread(buf2);
    assertEquals("Wrong number!", 30 + 10, tobj.available());
    try {
        tobj.unread(buf2);
        fail("IOException not thrown.");
    } catch (IOException e) {
    // expected
    }
    tobj.read(readBuf);
    assertEquals("Incorrect bytes read", str2, new String(readBuf));
    underlying.throwExceptionOnNextUse = true;
    try {
        tobj.read(buf2);
        fail("IOException not thrown.");
    } catch (IOException e) {
    // expected
    }
    // Test for method void java.io.PushbackInputStream.unread(byte [])
    try {
        byte[] buf = new byte[100];
        pis.read(buf, 0, buf.length);
        assertTrue("Incorrect bytes read", new String(buf).equals(fileString.substring(0, 100)));
        pis.unread(buf);
        pis.read(buf, 0, 50);
        assertTrue("Failed to unread bytes", new String(buf, 0, 50).equals(fileString.substring(0, 50)));
    } catch (IOException e) {
        fail("IOException during unread test : " + e.getMessage());
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Example 17 with PushbackInputStream

use of java.io.PushbackInputStream in project nutz by nutzam.

the class Streams method utf8filte.

/**
     * 判断并移除UTF-8的BOM头
     */
public static InputStream utf8filte(InputStream in) {
    try {
        if (in.available() == -1)
            return in;
        PushbackInputStream pis = new PushbackInputStream(in, 3);
        byte[] header = new byte[3];
        int len = pis.read(header, 0, 3);
        if (len < 1)
            return in;
        if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) {
            pis.unread(header, 0, len);
        }
        return pis;
    } catch (IOException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Example 18 with PushbackInputStream

use of java.io.PushbackInputStream in project XobotOS by xamarin.

the class JDKX509CertificateFactory method engineGenerateCRL.

/**
     * Generates a certificate revocation list (CRL) object and initializes
     * it with the data read from the input stream inStream.
     */
public CRL engineGenerateCRL(InputStream inStream) throws CRLException {
    if (currentCrlStream == null) {
        currentCrlStream = inStream;
        sCrlData = null;
        sCrlDataObjectCount = 0;
    } else if (// reset if input stream has changed
    currentCrlStream != inStream) {
        currentCrlStream = inStream;
        sCrlData = null;
        sCrlDataObjectCount = 0;
    }
    try {
        if (sCrlData != null) {
            if (sCrlDataObjectCount != sCrlData.size()) {
                return getCRL();
            } else {
                sCrlData = null;
                sCrlDataObjectCount = 0;
                return null;
            }
        }
        int limit = ProviderUtil.getReadLimit(inStream);
        PushbackInputStream pis = new PushbackInputStream(inStream);
        int tag = pis.read();
        if (tag == -1) {
            return null;
        }
        pis.unread(tag);
        if (// assume ascii PEM encoded.
        tag != 0x30) {
            return readPEMCRL(pis);
        } else {
            // lazy evaluate to help processing of large CRLs
            return readDERCRL(new ASN1InputStream(pis, limit, true));
        }
    } catch (CRLException e) {
        throw e;
    } catch (Exception e) {
        throw new CRLException(e.toString());
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) PushbackInputStream(java.io.PushbackInputStream) CRLException(java.security.cert.CRLException) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CRLException(java.security.cert.CRLException)

Example 19 with PushbackInputStream

use of java.io.PushbackInputStream in project OpenRefine by OpenRefine.

the class XmlImporter method wrapPrefixRemovingInputStream.

private static final InputStream wrapPrefixRemovingInputStream(InputStream inputStream) throws XMLStreamException, IOException {
    PushbackInputStream pis = new PushbackInputStream(inputStream);
    int b;
    int count = 0;
    while (count < 100 && (b = pis.read()) >= 0) {
        if (++count > 100) {
            throw new XMLStreamException("File starts with too much non-XML content to skip over");
        } else if (b == '<') {
            pis.unread(b);
            break;
        }
    }
    return pis;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) PushbackInputStream(java.io.PushbackInputStream)

Example 20 with PushbackInputStream

use of java.io.PushbackInputStream in project OpenRefine by OpenRefine.

the class ExcelImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    JSONArray sheetRecords = new JSONArray();
    JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
    try {
        if (fileRecords.size() > 0) {
            JSONObject firstFileRecord = fileRecords.get(0);
            File file = ImportingUtilities.getFile(job, firstFileRecord);
            InputStream is = new FileInputStream(file);
            if (!is.markSupported()) {
                is = new PushbackInputStream(is, 8);
            }
            try {
                Workbook wb = POIXMLDocument.hasOOXMLHeader(is) ? new XSSFWorkbook(is) : new HSSFWorkbook(new POIFSFileSystem(is));
                int sheetCount = wb.getNumberOfSheets();
                boolean hasData = false;
                for (int i = 0; i < sheetCount; i++) {
                    Sheet sheet = wb.getSheetAt(i);
                    int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
                    JSONObject sheetRecord = new JSONObject();
                    JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
                    JSONUtilities.safePut(sheetRecord, "rows", rows);
                    if (hasData) {
                        JSONUtilities.safePut(sheetRecord, "selected", false);
                    } else if (rows > 1) {
                        JSONUtilities.safePut(sheetRecord, "selected", true);
                        hasData = true;
                    }
                    JSONUtilities.append(sheetRecords, sheetRecord);
                }
            } finally {
                is.close();
            }
        }
    } catch (IOException e) {
        logger.error("Error generating parser UI initialization data for Excel file", e);
    } catch (IllegalArgumentException e) {
        logger.error("Error generating parser UI initialization data for Excel file (only Excel 97 & later supported)", e);
    } catch (POIXMLException e) {
        logger.error("Error generating parser UI initialization data for Excel file - invalid XML", e);
    }
    return options;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FileInputStream(java.io.FileInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) JSONObject(org.json.JSONObject) PushbackInputStream(java.io.PushbackInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)80 IOException (java.io.IOException)42 InputStream (java.io.InputStream)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileInputStream (java.io.FileInputStream)6 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 File (java.io.File)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 InputStreamReader (java.io.InputStreamReader)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Charset (java.nio.charset.Charset)2