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());
}
}
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);
}
}
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());
}
}
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;
}
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;
}
Aggregations