List of usage examples for java.io PushbackInputStream read
public int read() throws IOException
From source file:org.osaf.cosmo.cmp.CmpServlet.java
private Document readXmlRequest(HttpServletRequest req) throws SAXException, IOException { if (req.getContentLength() == 0) { return null; }/* www .ja v a 2 s . c om*/ InputStream in = req.getInputStream(); if (in == null) { return null; } // check to see if there's any data to read PushbackInputStream filtered = new PushbackInputStream(in, 1); int read = filtered.read(); if (read == -1) { return null; } filtered.unread(read); // there is data, so read the stream try { BUILDER_FACTORY.setNamespaceAware(true); DocumentBuilder docBuilder = BUILDER_FACTORY.newDocumentBuilder(); return docBuilder.parse(filtered); } catch (ParserConfigurationException e) { throw new CmpException("error configuring xml builder", e); } }
From source file:us.levk.math.linear.HugeRealMatrix.java
/** * Parse a matrix from a stream//from ww w .j a v a 2 s . c om * * @param stream * @param columnDelimiters * @param rowDelimiters * @param parser * @throws IOException * @throws ParseException */ public HugeRealMatrix(InputStream stream, char[] columnDelimiters, char[] rowDelimiters, NumberFormat parser) throws IOException, ParseException { StringBuilder word = new StringBuilder(); Set<Integer> columnDelims = new HashSet<>(); Set<Integer> rowDelims = new HashSet<>(); for (char b : columnDelimiters) columnDelims.add((int) b); for (char b : rowDelimiters) rowDelims.add((int) b); String name = randomFileName(); try (RandomAccessFile file = new RandomAccessFile(name, "rw")) { int columns = 0; int rows = 0; int prevColumns = -1; PushbackInputStream in = new PushbackInputStream(stream, 1024); startMatrixParse(in, columnDelims, rowDelims); boolean newRow = true; for (int c;;) { if (newRow) { startRowParse(in, rows, columnDelims, parser); newRow = false; } c = in.read(); if (columnDelims.contains(c) || rowDelims.contains(c) || c < 0) { try { double value = parser.parse(word.toString().trim()).doubleValue(); file.writeDouble(value); } catch (ParseException e) { if (columns == 0) break; else throw e; } columns++; word = new StringBuilder(); if (rowDelims.contains(c) || c < 0) { rows++; if (columns != prevColumns && prevColumns >= 0) throw new IOException("Jagged row detected, previous row " + prevColumns + " columns, current row " + columns + " columns"); prevColumns = columns; columns = 0; if (c < 0) break; else newRow = true; } } else word.append((char) c); } endMatrixParse(in, prevColumns, rows); this.name = name; this.file = file; this.columnDimension = prevColumns; this.rowDimension = rows; long size = 8L * rowDimension * columnDimension; for (long offset = 0; offset < size; offset += MAPPING_SIZE) mappings.add(file.getChannel().map(READ_WRITE, offset, min(size - offset, MAPPING_SIZE))); } catch (IOException | ParseException | RuntimeException | Error e) { new File(name).delete(); throw e; } }