Search in sources :

Example 91 with PushbackInputStream

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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 92 with PushbackInputStream

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;
}
Also used : FilenameFilter(java.io.FilenameFilter) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) PushbackInputStream(java.io.PushbackInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 93 with PushbackInputStream

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();
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 94 with PushbackInputStream

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());
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 95 with PushbackInputStream

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");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PushbackInputStream(java.io.PushbackInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)130 IOException (java.io.IOException)61 InputStream (java.io.InputStream)34 ByteArrayInputStream (java.io.ByteArrayInputStream)21 FileInputStream (java.io.FileInputStream)10 GZIPInputStream (java.util.zip.GZIPInputStream)10 InputStreamReader (java.io.InputStreamReader)8 File (java.io.File)5 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3