use of java.io.PushbackInputStream in project wso2-synapse by wso2.
the class HessianMessageBuilder method detectAndMarkMessageFault.
/**
* Reads the first four bytes of the inputstream to detect whether the message represents a
* fault message. Once a fault message has been detected, a property used to mark fault messages
* is stored in the Axis2 message context. The implementaton uses a PushbackInputStream to be
* able to put those four bytes back at the end of processing.
*
* @param messageContext the Axis2 message context
* @param inputStream the inputstream to read the Hessian message
*
* @return the wrapped (pushback) input stream
*
* @throws IOException if an I/O error occurs
*/
private PushbackInputStream detectAndMarkMessageFault(final MessageContext messageContext, final InputStream inputStream) throws IOException {
int bytesToRead = 4;
PushbackInputStream pis = new PushbackInputStream(inputStream, bytesToRead);
byte[] headerBytes = new byte[bytesToRead];
int n = pis.read(headerBytes);
// checking fourth byte for fault marker
if (n == bytesToRead) {
if (headerBytes[bytesToRead - 1] == HessianConstants.HESSIAN_V1_FAULT_IDENTIFIER || headerBytes[bytesToRead - 1] == HessianConstants.HESSIAN_V2_FAULT_IDENTIFIER) {
messageContext.setProperty(BaseConstants.FAULT_MESSAGE, SynapseConstants.TRUE);
if (log.isDebugEnabled()) {
log.debug("Hessian fault detected, marking in Axis2 message context");
}
}
pis.unread(headerBytes);
} else if (n > 0) {
byte[] bytesRead = new byte[n];
System.arraycopy(headerBytes, 0, bytesRead, 0, n);
pis.unread(bytesRead);
}
return pis;
}
use of java.io.PushbackInputStream in project jersey by jersey.
the class EntityInputStream method isEmpty.
/**
* Check if the underlying entity stream is empty.
* <p>
* Note that the operation may need to block until a first byte (or EOF) is available in the stream.
* </p>
*
* @return {@code true} if the entity stream is empty, {@code false} otherwise.
*/
public boolean isEmpty() {
ensureNotClosed();
final InputStream in = input;
if (in == null) {
return true;
}
try {
// Try #markSupported first - #available on WLS waits until socked timeout is reached when chunked encoding is used.
if (in.markSupported()) {
in.mark(1);
int i = in.read();
in.reset();
return i == -1;
} else {
try {
if (in.available() > 0) {
return false;
}
} catch (IOException ioe) {
// NOOP. Try other approaches as this can fail on WLS.
}
int b = in.read();
if (b == -1) {
return true;
}
PushbackInputStream pbis;
if (in instanceof PushbackInputStream) {
pbis = (PushbackInputStream) in;
} else {
pbis = new PushbackInputStream(in, 1);
input = pbis;
}
pbis.unread(b);
return false;
}
} catch (IOException ex) {
throw new ProcessingException(ex);
}
}
use of java.io.PushbackInputStream in project robovm by robovm.
the class ZipInputStream method closeEntry.
/**
* Closes the current zip entry and prepares to read the next entry.
*
* @throws IOException
* if an {@code IOException} occurs.
*/
public void closeEntry() throws IOException {
checkClosed();
if (currentEntry == null) {
return;
}
if (currentEntry instanceof java.util.jar.JarEntry) {
Attributes temp = ((JarEntry) currentEntry).getAttributes();
if (temp != null && temp.containsKey("hidden")) {
return;
}
}
/*
* The following code is careful to leave the ZipInputStream in a
* consistent state, even when close() results in an exception. It does
* so by:
* - pushing bytes back into the source stream
* - reading a data descriptor footer from the source stream
* - resetting fields that manage the entry being closed
*/
// Ensure all entry bytes are read
Exception failure = null;
try {
Streams.skipAll(this);
} catch (Exception e) {
failure = e;
}
int inB, out;
if (currentEntry.compressionMethod == ZipEntry.DEFLATED) {
inB = inf.getTotalIn();
out = inf.getTotalOut();
} else {
inB = inRead;
out = inRead;
}
int diff = entryIn - inB;
// Pushback any required bytes
if (diff != 0) {
((PushbackInputStream) in).unread(buf, len - diff, diff);
}
try {
readAndVerifyDataDescriptor(inB, out);
} catch (Exception e) {
if (failure == null) {
// otherwise we're already going to throw
failure = e;
}
}
inf.reset();
lastRead = inRead = entryIn = len = 0;
crc.reset();
currentEntry = null;
if (failure != null) {
if (failure instanceof IOException) {
throw (IOException) failure;
} else if (failure instanceof RuntimeException) {
throw (RuntimeException) failure;
}
AssertionError error = new AssertionError();
error.initCause(failure);
throw error;
}
}
use of java.io.PushbackInputStream in project OpenRefine by OpenRefine.
the class ExcelImporter method parseOneFile.
@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, JSONObject options, List<Exception> exceptions) {
Workbook wb = null;
if (!inputStream.markSupported()) {
inputStream = new PushbackInputStream(inputStream, 8);
}
try {
wb = POIXMLDocument.hasOOXMLHeader(inputStream) ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(new POIFSFileSystem(inputStream));
} catch (IOException e) {
exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Try to use Excel to re-save the file as a different Excel version or as TSV and upload again.", e));
return;
} catch (ArrayIndexOutOfBoundsException e) {
exceptions.add(new ImportException("Attempted to parse file as an Excel file but failed. " + "This is probably caused by a corrupt excel file, or due to the file having previously been created or saved by a non-Microsoft application. " + "Please try opening the file in Microsoft Excel and resaving it, then try re-uploading the file. " + "See https://issues.apache.org/bugzilla/show_bug.cgi?id=48261 for further details", e));
return;
} catch (IllegalArgumentException e) {
exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Only Excel 97 and later formats are supported.", e));
return;
} catch (POIXMLException e) {
exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Invalid XML.", e));
return;
}
int[] sheets = JSONUtilities.getIntArray(options, "sheets");
for (int sheetIndex : sheets) {
final Sheet sheet = wb.getSheetAt(sheetIndex);
final int lastRow = sheet.getLastRowNum();
TableDataReader dataReader = new TableDataReader() {
int nextRow = 0;
Map<String, Recon> reconMap = new HashMap<String, Recon>();
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (nextRow > lastRow) {
return null;
}
List<Object> cells = new ArrayList<Object>();
org.apache.poi.ss.usermodel.Row row = sheet.getRow(nextRow++);
if (row != null) {
short lastCell = row.getLastCellNum();
for (short cellIndex = 0; cellIndex < lastCell; cellIndex++) {
Cell cell = null;
org.apache.poi.ss.usermodel.Cell sourceCell = row.getCell(cellIndex);
if (sourceCell != null) {
cell = extractCell(sourceCell, reconMap);
}
cells.add(cell);
}
}
return cells;
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + sheet.getSheetName(), limit, options, exceptions);
}
}
use of java.io.PushbackInputStream in project neo4j by neo4j.
the class Readables method wrap.
public static CharReadable wrap(final InputStream stream, final String sourceName, Charset charset) throws IOException {
byte[] bytes = new byte[Magic.longest()];
PushbackInputStream pushbackStream = new PushbackInputStream(stream, bytes.length);
Charset usedCharset = charset;
int read = stream.read(bytes);
if (read >= 0) {
bytes = read < bytes.length ? Arrays.copyOf(bytes, read) : bytes;
Magic magic = Magic.of(bytes);
int excessiveBytes = read;
if (magic.impliesEncoding()) {
// Unread the diff between the BOM and the longest magic we gathered bytes for
excessiveBytes -= magic.length();
usedCharset = magic.encoding();
}
pushbackStream.unread(bytes, read - excessiveBytes, excessiveBytes);
}
return wrap(new InputStreamReader(pushbackStream, usedCharset) {
@Override
public String toString() {
return sourceName;
}
});
}
Aggregations