Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

In this page you can find the example usage for java.io InputStreamReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:com.apricot.eating.xml.XMLParser.java

private XMLParser(InputStream is) {
    try {//  w ww .  jav a2 s  . c  o m
        DocumentBuilderFactory factory = new DocumentBuilderFactoryImpl();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStreamReader isr = new InputStreamReader(is, "utf-8");

        StringBuffer sb = new StringBuffer();
        int b;
        while ((b = isr.read()) != -1) {
            sb.append((char) b);
        }
        is = new ByteArrayInputStream(sb.toString().getBytes("utf-8"));
        if (is != null) {
            document = builder.parse(is);
        } else {
            document = builder.newDocument();
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}

From source file:org.pwsafe.passwordsafeswt.dialog.LicenseDialog.java

private StringBuilder getLicence() {
    final StringBuilder licence = new StringBuilder(2048);
    final String licenceFileName = "LICENSE"; //$NON-NLS-1$
    URL address = getClass().getResource("../" + licenceFileName); //$NON-NLS-1$
    if (address == null) {
        address = getClass().getResource(licenceFileName);
    }/*from  w ww .  j a v a  2 s  .  co  m*/
    InputStream in = null;
    try {
        if (address == null) {
            address = new File(licenceFileName).toURI().toURL();
        }
        LOG.debug("License path " + address); //$NON-NLS-1$

        in = address.openStream();
        in = new BufferedInputStream(in);

        InputStreamReader inReader = null;
        inReader = new InputStreamReader(in, "UTF-8"); //$NON-NLS-1$
        while (true) {
            int c = inReader.read();
            if (c == -1)
                break;
            licence.append((char) c);
        }
        inReader.close();
    } catch (MalformedURLException e) {
        LOG.error(e);
    } catch (UnsupportedEncodingException e1) {
        LOG.error(e1);

    } catch (IOException e) {
        LOG.error(e);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
                LOG.warn("Exception on close" + e);
            }
    }
    return licence;
}

From source file:edu.umd.cfar.lamp.viper.util.StringHelp.java

/** 
 * Checks to see if the stream begins with an xml processing directive, eg
 * <code>&lt;?xml?&gt;</code>. This method does not check to see that the 
 * stream is well-formed, or even if the processing directive is good, just that
 * the first non-whitespace characters are "&lt;?xml".
 *
 * @param f The file to check for xml processing directive
 * @throws IOException if there is an error while reading the file, eg FileNotFoundException
 * @return <code>true</code> if the directive was found. 
 *//*  w  w w  .ja  v  a2  s. c o  m*/
public static boolean isXMLFormat(InputStream f) throws IOException {
    final int LIMIT = 4024;
    f.mark(LIMIT);
    InputStreamReader isr = new InputStreamReader(f);
    char n;
    do {
        n = (char) isr.read();
    } while (Character.isWhitespace(n));
    boolean xml = (n == '<' && isr.read() == '?' && (char) isr.read() == 'x' && (char) isr.read() == 'm'
            && (char) isr.read() == 'l');
    f.reset();
    return xml;
}

From source file:com.annuletconsulting.homecommand.node.AsyncSend.java

@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
    AsyncTaskLoader<String> loader = new AsyncTaskLoader<String>(activity) {
        @Override//from w  w  w.  java2s.  c  om
        public String loadInBackground() {
            StringBuffer instr = new StringBuffer();
            try {
                Socket connection = new Socket(ipAddr, port);
                BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
                OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
                osw.write(formatJSON(command.toUpperCase()));
                osw.write(13);
                osw.flush();
                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
                InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
                int c;
                while ((c = isr.read()) != 13)
                    instr.append((char) c);
                isr.close();
                bis.close();
                osw.close();
                bos.close();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return instr.toString();
        }
    };
    return loader;
}

From source file:eu.eexcess.opensearch.opensearchDescriptionDocument.parse.OpenSearchDocumentParserTest.java

License:asdf

/**
 * Parse XML from relativeFilepath and build an
 * {@link OpensearchDescription}//  w  ww .  j a va2  s.  com
 * 
 * @param relativeFilepath
 * @return
 */
private OpensearchDescription readFromXMLFile(String relativeFilepath) {
    InputStream inputFile = OpenSearchDocumentParserTest.class.getResourceAsStream(relativeFilepath);

    OpensearchDescription document = null;

    InputStreamReader fileReader = new InputStreamReader(inputFile);

    try {
        StringBuilder xmlDocumentDescription = new StringBuilder();
        int character = fileReader.read();
        while (character != -1) {
            xmlDocumentDescription.append((char) character);
            character = fileReader.read();
        }
        fileReader.close();

        OpenSearchDocumentParser parser = new OpenSearchDocumentParser();
        document = parser.toDescriptionDocument(xmlDocumentDescription.toString());

    } catch (IOException e) {
        logger.log(Level.ERROR, e);
    }
    return document;
}

From source file:com.jadarstudios.rankcapes.forge.cape.CapePack.java

/**
 * Parses the Zip file in memory./*  w  w w .  j a  v  a 2  s.c om*/
 *
 * @param input the bytes of a valid zip file
 */
private void parsePack(byte[] input) {
    try {
        ZipInputStream zipInput = new ZipInputStream(new ByteArrayInputStream(input));

        String metadata = "";

        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            String name = entry.getName();

            if (name.endsWith(".png")) {
                // remove file extension from the name.
                name = FilenameUtils.removeExtension(name);

                StaticCape cape = this.loadCape(name, zipInput);
                this.unprocessedCapes.put(name, cape);
            } else if (name.endsWith(".mcmeta")) {
                // parses the pack metadata.
                InputStreamReader streamReader = new InputStreamReader(zipInput);
                while (streamReader.ready()) {
                    metadata += (char) streamReader.read();
                }
            }
        }
        if (!Strings.isNullOrEmpty(metadata)) {
            this.parsePackMetadata(StringUtils.remove(metadata, (char) 65535));
        } else {
            RankCapesForge.log.warn("Cape Pack metadata is missing!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.micromata.genome.tpsb.builder.ScenarioLoaderContext.java

public String getScenarioFileContent(String file, String encoding, int maxLength) {
    try {/* ww w.  ja v a  2s .co m*/
        InputStream instr = openInputStream(file);
        InputStreamReader sr = new InputStreamReader(instr, encoding);
        StringBuilder ret = new StringBuilder(maxLength);
        int ch;
        int count = 0;
        while ((ch = sr.read()) != -1) {
            ret.append((char) ch);
            ++count;
            if (count > maxLength) {
                break;
            }
        }
        return ret.toString();
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.polymap.core.mapeditor.contextmenu.WmsFeatureInfoContribution.java

protected String issueRequest(IGeoResource geores, String format, boolean handleError) {
    try {//from  w w w  .java2s.c  o  m
        WebMapServer wms = geores.resolve(WebMapServer.class, null);
        GetMapRequest mapRequest = wms.createGetMapRequest();
        Layer wmsLayer = geores.resolve(Layer.class, null);
        mapRequest.addLayer(wmsLayer);
        mapRequest.setBBox(site.getMapExtent());
        mapRequest.setSRS(site.getMap().getCRSCode());
        mapRequest.setDimensions(site.getMapSize().x, site.getMapSize().y);

        GetFeatureInfoRequest featureInfoRequest = wms.createGetFeatureInfoRequest(mapRequest);
        featureInfoRequest.setFeatureCount(100);
        if (format != null) {
            featureInfoRequest.setInfoFormat(format);
        }
        featureInfoRequest.setQueryLayers(Collections.singleton(wmsLayer));
        featureInfoRequest.setQueryPoint(site.widgetMousePosition().x, site.widgetMousePosition().y);

        log.debug("URL: " + featureInfoRequest.getFinalURL());

        GetFeatureInfoResponse response = wms.issueRequest(featureInfoRequest);
        InputStream in = response.getInputStream();
        try {
            //log.info( "   contentType:" + response.getContentType() );
            // XXX charset from contentType
            InputStreamReader reader = new InputStreamReader(in, "UTF-8");

            StringBuilder content = new StringBuilder(4096);
            for (int c = reader.read(); c != -1; c = reader.read()) {
                content.append((char) c);
            }
            log.debug(content.toString());
            return content.toString();
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (Exception e) {
        log.warn("Unable to GetFeatureInfo: " + e.getLocalizedMessage());
        log.debug("", e);
        if (handleError) {
            PolymapWorkbench.handleError(MapEditorPlugin.PLUGIN_ID, this, "", e);
        }
        return "";
    }
}