use of java.io.PushbackInputStream in project processdash by dtuma.
the class DefectLog method readDefects.
public Defect[] readDefects() {
Defect[] results = null;
PushbackInputStream pin = null;
try {
pin = new PushbackInputStream(new FileInputStream(defectLogFilename));
int c = pin.read();
if (c == -1) {
// empty file
;
} else if (c == '<') {
// xml format
pin.unread(c);
results = readDefectsFromXml(pin);
} else {
// tab-delimited format
pin.unread(c);
results = readTabDelimitedDefects(pin);
}
} catch (FileNotFoundException f) {
System.out.println("FileNotFoundException: " + f);
} catch (SAXException i) {
System.out.println("Invalid XML defect data in file " + defectLogFilename + ": " + XMLUtils.exceptionMessage(i));
} catch (IOException i) {
System.out.println("IOException: " + i);
}
FileUtils.safelyClose(pin);
if (results == null)
results = new Defect[0];
return results;
}
use of java.io.PushbackInputStream in project processdash by dtuma.
the class BrokenDataFileHandler method findReplacementFromBackup.
private InputStream findReplacementFromBackup(File f) {
// look for a "backup" subdirectory
File backupDir = new File(f.getParentFile(), "backup");
if (!backupDir.isDirectory())
return null;
// find a list of ZIP file in that directory, and look inside each
// one (starting with the most recent) for a copy of the given file.
File[] backupZipFiles = backupDir.listFiles(new FilenameFilter() {
public boolean accept(File location, String name) {
return name.startsWith("pdash-") && name.endsWith(".zip");
}
});
Arrays.sort(backupZipFiles);
for (int i = backupZipFiles.length; i-- > 0; ) {
try {
File zip = backupZipFiles[i];
ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zip)));
ZipEntry e;
while ((e = in.getNextEntry()) != null) {
if (e.getName().equalsIgnoreCase(f.getName())) {
PushbackInputStream pb = new PushbackInputStream(in, 1);
int b = pb.read();
if (b == 0)
// abort and try the next ZIP.
break;
// otherwise, we've found a viable historical file.
System.out.println("*************** REPAIRING CORRUPT " + f.getName() + " with data from backup/" + zip.getName() + " *****************");
pb.unread(b);
return pb;
}
}
in.close();
} catch (IOException ioe) {
// if a given ZIP file is damaged or corrupt, skip it and look
// through the previous ZIP files to scan further back in time
}
}
// no replacement file was found in the recent backups.
System.out.println("*************** FILE " + f.getName() + " APPEARS TO BE CORRUPT *****************");
return null;
}
use of java.io.PushbackInputStream in project frostwire by frostwire.
the class HTTPConnectionImpl method connectInputStream.
protected synchronized void connectInputStream() throws IOException {
if (this.inputStreamConnected) {
return;
}
this.inputStreamConnected = true;
/* first read http header */
ByteBuffer header = HTTPConnectionUtils.readheader(this.httpSocket.getInputStream(), true);
byte[] bytes = new byte[header.limit()];
header.get(bytes);
this.httpHeader = new String(bytes, "ISO-8859-1").trim();
/* parse response code/message */
if (this.httpHeader.startsWith("HTTP")) {
final String code = new Regex(this.httpHeader, "HTTP.*? (\\d+)").getMatch(0);
if (code != null) {
this.httpResponseCode = Integer.parseInt(code);
}
this.httpResponseMessage = new Regex(this.httpHeader, "HTTP.*? \\d+ (.+)").getMatch(0);
if (this.httpResponseMessage == null) {
this.httpResponseMessage = "";
}
} else {
this.httpHeader = "unknown HTTP response";
this.httpResponseCode = 200;
this.httpResponseMessage = "unknown HTTP response";
if (bytes.length > 0) {
this.inputStream = new PushbackInputStream(this.httpSocket.getInputStream(), bytes.length);
/*
* push back the data that got read because no http header
* exists
*/
((PushbackInputStream) this.inputStream).unread(bytes);
} else {
/* nothing to push back */
this.inputStream = this.httpSocket.getInputStream();
}
return;
}
/* read rest of http headers */
header = HTTPConnectionUtils.readheader(this.httpSocket.getInputStream(), false);
bytes = new byte[header.limit()];
header.get(bytes);
String temp = new String(bytes, "UTF-8");
/* split header into single strings, use RN or N(buggy fucking non rfc) */
String[] headerStrings = temp.split("(\r\n)|(\n)");
temp = null;
for (final String line : headerStrings) {
String key = null;
String value = null;
int index = 0;
if ((index = line.indexOf(": ")) > 0) {
key = line.substring(0, index);
value = line.substring(index + 2);
} else if ((index = line.indexOf(":")) > 0) {
/* buggy servers that don't have :space ARG */
key = line.substring(0, index);
value = line.substring(index + 1);
} else {
key = null;
value = line;
}
if (key != null) {
key = key.trim();
}
if (value != null) {
value = value.trim();
}
List<String> list = this.headers.get(key);
if (list == null) {
list = new ArrayList<String>();
this.headers.put(key, list);
}
list.add(value);
}
headerStrings = null;
final List<String> chunked = this.headers.get("Transfer-Encoding");
if (chunked != null && chunked.size() > 0 && "chunked".equalsIgnoreCase(chunked.get(0))) {
this.inputStream = new ChunkedInputStream(this.httpSocket.getInputStream());
} else {
this.inputStream = this.httpSocket.getInputStream();
}
}
use of java.io.PushbackInputStream in project dubbo by alibaba.
the class StreamUtilsTest method testMarkSupportedInputStream.
@Test
public void testMarkSupportedInputStream() throws Exception {
InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt");
assertEquals(10, is.available());
is = new PushbackInputStream(is);
assertEquals(10, is.available());
assertFalse(is.markSupported());
is = StreamUtils.markSupportedInputStream(is);
assertEquals(10, is.available());
is.mark(0);
assertEquals((int) '0', is.read());
assertEquals((int) '1', is.read());
is.reset();
assertEquals((int) '0', is.read());
assertEquals((int) '1', is.read());
assertEquals((int) '2', is.read());
is.mark(0);
assertEquals((int) '3', is.read());
assertEquals((int) '4', is.read());
assertEquals((int) '5', is.read());
is.reset();
assertEquals((int) '3', is.read());
assertEquals((int) '4', is.read());
is.mark(0);
assertEquals((int) '5', is.read());
assertEquals((int) '6', is.read());
is.reset();
assertEquals((int) '5', is.read());
assertEquals((int) '6', is.read());
assertEquals((int) '7', is.read());
assertEquals((int) '8', is.read());
assertEquals((int) '9', is.read());
assertEquals(-1, is.read());
assertEquals(-1, is.read());
is.mark(0);
assertEquals(-1, is.read());
assertEquals(-1, is.read());
is.reset();
assertEquals(-1, is.read());
assertEquals(-1, is.read());
}
use of java.io.PushbackInputStream in project chipKIT32-MAX by chipKIT32.
the class StdXMLReader method stream2reader.
/**
* Converts a stream to a reader while detecting the encoding.
*
* @param stream the input for the XML data.
* @param charsRead buffer where to put characters that have been read
*
* @throws java.io.IOException
* if an I/O error occurred
*/
protected Reader stream2reader(InputStream stream, StringBuffer charsRead) throws IOException {
PushbackInputStream pbstream = new PushbackInputStream(stream);
int b = pbstream.read();
switch(b) {
case 0x00:
case 0xFE:
case 0xFF:
pbstream.unread(b);
return new InputStreamReader(pbstream, "UTF-16");
case 0xEF:
for (int i = 0; i < 2; i++) {
pbstream.read();
}
return new InputStreamReader(pbstream, "UTF-8");
case 0x3C:
b = pbstream.read();
charsRead.append('<');
while ((b > 0) && (b != 0x3E)) {
charsRead.append((char) b);
b = pbstream.read();
}
if (b > 0) {
charsRead.append((char) b);
}
String encoding = this.getEncoding(charsRead.toString());
if (encoding == null) {
return new InputStreamReader(pbstream, "UTF-8");
}
charsRead.setLength(0);
try {
return new InputStreamReader(pbstream, encoding);
} catch (UnsupportedEncodingException e) {
return new InputStreamReader(pbstream, "UTF-8");
}
default:
charsRead.append((char) b);
return new InputStreamReader(pbstream, "UTF-8");
}
}
Aggregations