Search in sources :

Example 76 with BufferedInputStream

use of java.io.BufferedInputStream in project bazel by bazelbuild.

the class DataValueFileWithIds method parse.

public static void parse(XMLInputFactory xmlInputFactory, Path source, FullyQualifiedName fileKey, FullyQualifiedName.Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws IOException, XMLStreamException {
    ImmutableSet.Builder<String> newIds = ImmutableSet.builder();
    try (BufferedInputStream inStream = new BufferedInputStream(Files.newInputStream(source))) {
        XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(inStream, StandardCharsets.UTF_8.toString());
        // forgiving and allow even non-android namespaced attributes to define a new ID.
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                StartElement start = event.asStartElement();
                Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(start);
                while (attributes.hasNext()) {
                    Attribute attribute = attributes.next();
                    String value = attribute.getValue();
                    if (value.startsWith(SdkConstants.NEW_ID_PREFIX)) {
                        String idName = value.substring(SdkConstants.NEW_ID_PREFIX.length());
                        newIds.add(idName);
                    }
                }
            }
        }
        eventReader.close();
    } catch (XMLStreamException e) {
        throw new XMLStreamException(source + ": " + e.getMessage(), e.getLocation(), e);
    } catch (RuntimeException e) {
        throw new RuntimeException("Error parsing " + source, e);
    }
    ImmutableSet<String> idResources = newIds.build();
    overwritingConsumer.consume(fileKey, DataValueFile.of(source));
    for (String id : idResources) {
        combiningConsumer.consume(fqnFactory.create(ResourceType.ID, id), DataResourceXml.createWithNoNamespace(source, IdXmlResourceValue.of()));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) ImmutableSet(com.google.common.collect.ImmutableSet) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 77 with BufferedInputStream

use of java.io.BufferedInputStream in project camel by apache.

the class ManualGenerator method grabBodyContent.

private String grabBodyContent() throws MalformedURLException, IOException {
    URL url = new URL(page);
    File file = new File(targetDir, ".manualCache-" + url.getFile().substring(1));
    try {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        XMLReader parser = new Parser();
        parser.setFeature(Parser.namespacesFeature, false);
        parser.setFeature(Parser.namespacePrefixesFeature, false);
        parser.setProperty(Parser.schemaProperty, new org.ccil.cowan.tagsoup.HTMLSchema() {

            {
                //problem with nested lists that the confluence {toc} macro creates
                elementType("ul", M_LI, M_BLOCK | M_LI, 0);
            }
        });
        StringWriter w = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(w) {

            int inDiv = Integer.MAX_VALUE;

            int count;

            public void characters(char[] ch, int start, int len) throws SAXException {
                if (inDiv <= count) {
                    super.characters(ch, start, len);
                }
            }

            public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
                count++;
                if ("div".equalsIgnoreCase(qName) && "wiki-content maincontent".equalsIgnoreCase(atts.getValue("class"))) {
                    inDiv = count;
                }
                if (inDiv <= count) {
                    super.startElement(uri, localName, qName, atts);
                }
            }

            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (inDiv <= count) {
                    super.endElement(uri, localName, qName);
                }
                count--;
                if (inDiv > count) {
                    inDiv = Integer.MAX_VALUE;
                }
            }
        };
        xmlWriter.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
        xmlWriter.setOutputProperty(XMLWriter.METHOD, "html");
        parser.setContentHandler(xmlWriter);
        long date = con.getLastModified();
        parser.parse(new InputSource(new BufferedInputStream(con.getInputStream())));
        FileWriter writer = new FileWriter(file);
        writer.write(Long.toString(date));
        writer.close();
        return w.toString();
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException("Failed", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) FileWriter(java.io.FileWriter) Attributes(org.xml.sax.Attributes) XMLWriter(org.ccil.cowan.tagsoup.XMLWriter) URL(java.net.URL) Parser(org.ccil.cowan.tagsoup.Parser) HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) XMLReader(org.xml.sax.XMLReader)

Example 78 with BufferedInputStream

use of java.io.BufferedInputStream in project android-classyshark by google.

the class MetaObjectFactory method getMetaObjectFromAar.

private static MetaObject getMetaObjectFromAar(String className, File archiveFile) {
    try {
        File file = File.createTempFile("classes", "jar");
        file.deleteOnExit();
        OutputStream out = new FileOutputStream(file);
        FileInputStream fin = new FileInputStream(archiveFile);
        BufferedInputStream bin = new BufferedInputStream(fin);
        ZipInputStream zin = new ZipInputStream(bin);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.getName().endsWith(".jar")) {
                byte[] buffer = new byte[8192];
                int len;
                while ((len = zin.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.close();
                MetaObject result = getMetaObjectFromJar(className, file);
                return result;
            }
        }
    } catch (Exception e) {
    }
    return new MetaObjectClass(Exception.class);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) MetaObjectClass(com.google.classyshark.silverghost.translator.java.clazz.reflect.MetaObjectClass) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException)

Example 79 with BufferedInputStream

use of java.io.BufferedInputStream in project android-classyshark by google.

the class AarReader method read.

@Override
public void read() {
    try {
        File file = File.createTempFile("classes", "jar");
        file.deleteOnExit();
        OutputStream out = new FileOutputStream(file);
        FileInputStream fin = new FileInputStream(binaryArchive);
        BufferedInputStream bin = new BufferedInputStream(fin);
        ZipInputStream zin = new ZipInputStream(bin);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.getName().endsWith(".jar")) {
                byte[] buffer = new byte[8192];
                int len;
                while ((len = zin.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                allClassNames.addAll(JarReader.readClassNamesFromJar(file, this.components));
            }
            if (ze.getName().equals("AndroidManifest.xml")) {
                allClassNames.add("AndroidManifest.xml");
            }
        }
        out.close();
        zin.closeEntry();
        zin.close();
    } catch (Exception e) {
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 80 with BufferedInputStream

use of java.io.BufferedInputStream in project android-classyshark by google.

the class DexLoaderBuilder method prepareDex.

private static boolean prepareDex(byte[] bytes, File dexInternalStoragePath) {
    BufferedInputStream bis = null;
    OutputStream dexWriter = null;
    try {
        bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
        dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
        byte[] buf = new byte[BUF_SIZE];
        int len;
        while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
            dexWriter.write(buf, 0, len);
        }
        dexWriter.close();
        bis.close();
        return true;
    } catch (IOException e) {
        if (dexWriter != null) {
            try {
                dexWriter.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
        return false;
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56