Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

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

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:net.rptools.maptool.launcher.MapToolLauncher.java

private void appendFileToWriter(File from, Writer out) {
    BufferedReader br;
    try {//from w w w. j  ava 2  s. c o  m
        br = new BufferedReader(new FileReader(from));
        try {
            final char[] buff = new char[1024];
            while (true) {
                int bytes;
                bytes = br.read(buff);
                if (bytes < 0) {
                    break;
                }
                out.write(buff, 0, bytes);
            }
        } catch (final IOException e) {
            logMsg(Level.SEVERE, "Error reading logging configuration file {0}", "msg.error.loggingUnreadable", //$NON-NLS-1$
                    from, e);
        } finally {
            CopiedFromOtherJars.closeQuietly(br);
        }
    } catch (final FileNotFoundException e) {
        logMsg(Level.SEVERE, "Error opening logging configuration file {0}", "msg.error.loggingFileNotFound", //$NON-NLS-1$
                from, e);
    }
    return;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReader.java

private void decodeNumberOfVariables(BufferedReader reader) throws IOException {
    if (reader == null) {
        throw new IllegalArgumentException("decodeNumberOfVariables: reader == null!");
    }/*from  ww w.ja v a 2  s .  c o m*/

    String temp = null;
    char[] tmp = new char[1];
    StringBuilder sb = new StringBuilder();

    while (reader.read(tmp) > 0) {
        temp = Character.toString(tmp[0]);
        if (temp.equals("/")) {
            break;
        } else {
            sb.append(temp);
        }
    }

    String rawNumberOfVariables = sb.toString();
    int rawLength = rawNumberOfVariables.length();

    String numberOfVariables = StringUtils.stripStart((StringUtils.strip(rawNumberOfVariables)), "0");

    if ((numberOfVariables.equals("")) && (numberOfVariables.length() == rawLength)) {
        numberOfVariables = "0";
    }

    varQnty = Integer.valueOf(numberOfVariables, 30);
    dataTable.setVarQuantity(Long.valueOf(numberOfVariables, 30));
}

From source file:org.fornax.cartridges.sculptor.smartclient.server.ScServlet.java

private void printRequest(HttpServletRequest request) throws IOException {
    Enumeration<String> paramList = request.getParameterNames();
    StringBuilder out = new StringBuilder();
    out.append("<<<<<<<<<<<< Request ");
    while (paramList.hasMoreElements()) {
        String param = paramList.nextElement();
        out.append(" :: " + param + "=" + request.getParameter(param));
    }/*from  w  w w .j  a v a  2 s  .  c om*/
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
    char[] dataBuff = new char[1024];
    int readBytes = bufferedReader.read(dataBuff);
    while (readBytes != -1) {
        out.append("\nData: " + new String(dataBuff, 0, readBytes));
        readBytes = bufferedReader.read(dataBuff);
    }
    log.log(Level.INFO, "{0}", out);
}

From source file:org.jets3t.service.impl.rest.XmlResponsesSaxParser.java

protected InputStream sanitizeXmlDocument(DefaultHandler handler, InputStream inputStream)
        throws ServiceException {
    if (!properties.getBoolProperty("xmlparser.sanitize-listings", true)) {
        // No sanitizing will be performed, return the original input stream unchanged.
        return inputStream;
    } else {//from w  w  w  .j ava2 s. c o  m
        if (log.isDebugEnabled()) {
            log.debug("Sanitizing XML document destined for handler " + handler.getClass());
        }

        InputStream sanitizedInputStream = null;

        try {
            /* Read object listing XML document from input stream provided into a
             * string buffer, so we can replace troublesome characters before
             * sending the document to the XML parser.
             */
            StringBuilder listingDocBuffer = new StringBuilder();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(inputStream, Constants.DEFAULT_ENCODING));

            char[] buf = new char[8192];
            int read = -1;
            while ((read = br.read(buf)) != -1) {
                listingDocBuffer.append(buf, 0, read);
            }
            br.close();

            // Replace any carriage return (\r) characters with explicit XML
            // character entities, to prevent the SAX parser from
            // misinterpreting 0x0D characters as 0x0A.
            String listingDoc = listingDocBuffer.toString().replaceAll("\r", "&#013;");

            sanitizedInputStream = new ByteArrayInputStream(listingDoc.getBytes(Constants.DEFAULT_ENCODING));
        } catch (Throwable t) {
            try {
                inputStream.close();
            } catch (IOException e) {
                if (log.isErrorEnabled()) {
                    log.error("Unable to close response InputStream after failure sanitizing XML document", e);
                }
            }
            throw new ServiceException(
                    "Failed to sanitize XML document destined for handler " + handler.getClass(), t);
        }
        return sanitizedInputStream;
    }
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.por.PORFileReader.java

/**
 * Read the given SPSS POR-format file via a <code>BufferedInputStream</code>
 * object.  This method calls an appropriate method associated with the given 
 * field header by reflection./* w ww  . jav a  2 s.  c o  m*/
 * 
 * @param stream a <code>BufferedInputStream</code>.
 * @return an <code>SDIOData</code> object
 * @throws java.io.IOException if an reading error occurs.
 */
@Override
public SDIOData read(BufferedInputStream stream, File dataFile) throws IOException {

    if (dataFile != null) {
        throw new IOException("this plugin does not support external raw data files");
    }

    File tempPORfile = decodeHeader(stream);
    BufferedReader bfReader = null;

    try {
        bfReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(tempPORfile.getAbsolutePath()), "US-ASCII"));
        if (bfReader == null) {
            dbgLog.fine("bfReader is null");
            throw new IOException("bufferedReader is null");
        }

        decodeSec2(bfReader);

        while (true) {

            char[] header = new char[LENGTH_SECTION_HEADER]; // 1 byte
            bfReader.read(header);
            String headerId = Character.toString(header[0]);

            dbgLog.fine("////////////////////// headerId=" + headerId + "//////////////////////");

            if (headerId.equals("Z")) {
                throw new IOException("reading failure: wrong headerId(Z) here");
            }

            if (headerId.equals("F")) {
                // missing value
                if ((missingValueTable != null) && (missingValueTable.size() > 0)) {
                    processMissingValueData();
                }
            }

            if (headerId.equals("8") && isCurrentVariableString) {
                headerId = "8S";
            }

            decode(headerId, bfReader);

            // for last iteration
            if (headerId.equals("F")) {
                // finished the last block (F == data) 
                // without reaching the end of this file.
                break;
            }
        }

        // post-parsing processing
        // save metadata to smd 
        // varialbe Name

        smd.setVariableName(variableNameList.toArray(new String[variableNameList.size()]));
        smd.setVariableLabel(variableLabelMap);
        smd.setMissingValueTable(missingValueTable);
        dbgLog.finer("*************** missingValueCodeTable ***************:\n" + missingValueCodeTable);
        smd.setInvalidDataTable(invalidDataTable);
        smd.setValueLabelTable(valueLabelTable);
        /* Final correction of the "variable type list" values: 
         * The date/time values are stored as character strings by the DVN, 
         * so the type information needs to be adjusted accordingly:
         *          -- L.A., v3.6 
         */
        int[] variableTypeMinimal = ArrayUtils
                .toPrimitive(variableTypelList.toArray(new Integer[variableTypelList.size()]));

        for (int indx = 0; indx < variableTypelList.size(); indx++) {
            int simpleType = 0;
            if (variableTypelList.get(indx) != null) {
                simpleType = variableTypelList.get(indx).intValue();
            }

            if (simpleType <= 0) {
                // NOT marked as a numeric at this point;
                // but is it a date/time/etc.?
                String variableFormatType = variableFormatTypeList[indx];
                if (variableFormatType != null
                        && (variableFormatType.equals("time") || variableFormatType.equals("date"))) {
                    variableTypeMinimal[indx] = 1;
                }
            }
        }

        smd.setVariableTypeMinimal(variableTypeMinimal);

        //smd.setVariableTypeMinimal(ArrayUtils.toPrimitive(variableTypelList.toArray(new Integer[variableTypelList.size()])));
        smd.setVariableFormat(printFormatList);
        smd.setVariableFormatName(printFormatNameTable);
        smd.setVariableFormatCategory(formatCategoryTable);
        smd.setValueLabelMappingTable(valueVariableMappingTable);

    } finally {
        try {
            if (bfReader != null) {
                bfReader.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (tempPORfile.exists()) {
            tempPORfile.delete();
        }
    }

    dbgLog.info("***** PORFileReader: read() end *****");
    return new SDIOData(smd, porDataSection);
}

From source file:org.kaaproject.kaa.server.control.cli.ControlServerCliIT.java

/**
 * Gets the test file content.//from ww w .  ja  v a  2  s  . com
 *
 * @param file the file
 * @return the test file content
 */
private String getTestFileContent(String file) {
    String targetPath = System.getProperty("targetPath");
    File targetDir = new File(targetPath);
    File testFile = new File(targetDir.getAbsolutePath() + File.separator + "test-classes" + File.separator
            + "data" + File.separator + file);
    BufferedReader reader = null;
    String result = null;
    try {
        StringWriter stringWriter = new StringWriter();
        reader = new BufferedReader(new FileReader(testFile));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            stringWriter.write(buf, 0, numRead);
        }
        result = stringWriter.toString();

    } catch (IOException e) {
        logger.error("Unable to read from specified file '" + testFile + "'! Error: " + e.getMessage(), e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
                logger.error("Unable to close file '" + testFile + "'! Error: " + ioe.getMessage(), ioe);
            }
        }
    }
    return result;
}

From source file:org.eclipsetrader.yahoo.internal.core.connector.StreamingConnector.java

@Override
public void run() {
    BufferedReader in = null;
    char[] buffer = new char[512];

    try {// w w w.ja  va  2  s  .  co m
        HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, Util.streamingFeedHost);

        HttpMethod method = null;

        while (!isStopping()) {
            // Check if the connection was not yet initialized or there are changed in the subscriptions.
            if (in == null || isSubscriptionsChanged()) {
                try {
                    if (method != null) {
                        method.releaseConnection();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e) {
                    // We can't do anything at this time, ignore
                }

                String[] symbols;
                synchronized (symbolSubscriptions) {
                    Set<String> s = new HashSet<String>(symbolSubscriptions.keySet());
                    s.add("MSFT");
                    symbols = s.toArray(new String[s.size()]);
                    setSubscriptionsChanged(false);
                    if (symbols.length == 0) {
                        break;
                    }
                }
                method = Util.getStreamingFeedMethod(symbols);

                client.executeMethod(method);

                in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));

                line = new StringBuilder();
                script = new StringBuilder();
                inTag = false;
                inScript = false;

                fetchLatestSnapshot(client, symbols, false);
            }

            if (in.ready()) {
                int length = in.read(buffer);
                if (length == -1) {
                    in.close();
                    in = null;
                    continue;
                }
                processIncomingChars(buffer, length);
            } else {
                // Check stale data
                List<String> updateList = new ArrayList<String>();
                synchronized (symbolSubscriptions) {
                    long currentTime = System.currentTimeMillis();
                    for (FeedSubscription subscription : symbolSubscriptions.values()) {
                        long elapsedTime = currentTime - subscription.getIdentifierType().getLastUpdate();
                        if (elapsedTime >= 60000) {
                            updateList.add(subscription.getIdentifierType().getSymbol());
                            subscription.getIdentifierType().setLastUpdate(currentTime / 60000 * 60000);
                        }
                    }
                }
                if (updateList.size() != 0) {
                    fetchLatestSnapshot(client, updateList.toArray(new String[updateList.size()]), true);
                }
            }

            Thread.sleep(100);
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            // We can't do anything at this time, ignore
        }
    }
}

From source file:eu.vital.TrustManager.connectors.dms.DMSManager.java

private String queryDMSTest(String dms_endpoint, String body) throws UnsupportedEncodingException, IOException,
        KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

    HttpURLConnection connection = null;
    // Of course everything will go over HTTPS (here we trust anything, we do not check the certificate)
    SSLContext sc = null;/*w w  w  . ja  va2 s  .c o  m*/
    try {
        sc = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException e1) {

    }

    InputStream is;
    BufferedReader rd;
    char cbuf[] = new char[1000000];
    int len;

    String urlParameters = body; // test cookie is the user performing the evalaution
    // The array of resources to evaluate policies on must be included

    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
    int postDataLength = postData.length;
    URL url = new URL(dms_URL + "/" + dms_endpoint);
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");

        connection.setUseCaches(false);
        connection.setDoOutput(true);

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.setRequestProperty("Cookie", cookie); // Include cookies (permissions evaluated for normal user, advanced user has the rights to evaluate)

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(postData);
        wr.close();

        // Get Response
        int err = connection.getResponseCode();
        if (err >= 200 && err < 300) {

            is = connection.getInputStream();
            //             
            rd = new BufferedReader(new InputStreamReader(is));
            //                //StringBuilder rd2 = new StringBuilder();

            len = rd.read(cbuf);
            String resp2 = String.valueOf(cbuf).substring(0, len - 1);
            rd.close();
            return resp2;

            //            char[] buffer = new char[1024*1024];
            //            StringBuilder output = new StringBuilder();
            //            int readLength = 0;
            //            while (readLength != -1) {
            //                readLength = rd.read(buffer, 0, buffer.length);
            //                if (readLength != -1) {
            //                    output.append(buffer, 0, readLength);
            //                }
            //            }

            //                return output.toString();
        }

    } catch (Exception e) {
        throw new java.net.ConnectException();
        //log 
    } finally {
        if (connection != null) {
            connection.disconnect();

        }
    }
    return null;
}

From source file:com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.java

protected InputStream sanitizeXmlDocument(DefaultHandler handler, InputStream inputStream) throws IOException {

    if (!sanitizeXmlDocument) {
        // No sanitizing will be performed, return the original input stream unchanged.
        return inputStream;
    } else {//from  w  w  w.jav  a  2  s. co  m
        if (log.isDebugEnabled()) {
            log.debug("Sanitizing XML document destined for handler " + handler.getClass());
        }

        InputStream sanitizedInputStream = null;

        try {

            /*
             * Read object listing XML document from input stream provided into a
             * string buffer, so we can replace troublesome characters before
             * sending the document to the XML parser.
             */
            StringBuilder listingDocBuffer = new StringBuilder();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(inputStream, Constants.DEFAULT_ENCODING));

            char[] buf = new char[8192];
            int read = -1;
            while ((read = br.read(buf)) != -1) {
                listingDocBuffer.append(buf, 0, read);
            }
            br.close();

            /*
             * Replace any carriage return (\r) characters with explicit XML
             * character entities, to prevent the SAX parser from
             * misinterpreting 0x0D characters as 0x0A and being unable to
             * parse the XML.
             */
            String listingDoc = listingDocBuffer.toString().replaceAll("\r", "&#013;");

            sanitizedInputStream = new ByteArrayInputStream(listingDoc.getBytes(UTF8));

        } catch (IOException e) {
            throw e;

        } catch (Throwable t) {
            try {
                inputStream.close();
            } catch (IOException e) {
                if (log.isErrorEnabled()) {
                    log.error("Unable to close response InputStream after failure sanitizing XML document", e);
                }
            }
            throw new AmazonClientException(
                    "Failed to sanitize XML document destined for handler " + handler.getClass(), t);
        }
        return sanitizedInputStream;
    }
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReader.java

@Override
public TabularDataIngest read(BufferedInputStream stream, File additionalData) throws IOException {
    dbgLog.fine("PORFileReader: read() start");

    if (additionalData != null) {
        //throw new IOException ("this plugin does not support external raw data files");
        dbgLog.fine("Using extended variable labels from file " + additionalData.getName());

        extendedLabels = createLabelMap(additionalData);
    }/*w  w w  .  j  av a 2 s  .  c  o  m*/

    File tempPORfile = decodeHeader(stream);
    BufferedReader bfReader = null;

    try {
        bfReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(tempPORfile.getAbsolutePath()), "US-ASCII"));
        if (bfReader == null) {
            dbgLog.fine("bfReader is null");
            throw new IOException("bufferedReader is null");
        }

        decodeSec2(bfReader);

        while (true) {

            char[] header = new char[LENGTH_SECTION_HEADER]; // 1 byte
            bfReader.read(header);
            String headerId = Character.toString(header[0]);

            dbgLog.fine("////////////////////// headerId=" + headerId + "//////////////////////");

            if (headerId.equals("Z")) {
                throw new IOException("reading failure: wrong headerId(Z) here");
            }

            if (headerId.equals("F")) {
                // missing value
                if ((missingValueTable != null) && (missingValueTable.size() > 0)) {
                    processMissingValueData();
                }
            }

            if (headerId.equals("8") && isCurrentVariableString) {
                headerId = "8S";
            }

            decode(headerId, bfReader);

            // for last iteration
            if (headerId.equals("F")) {
                // finished the last block (F == data) 
                // without reaching the end of this file.
                break;
            }
        }

    } finally {
        try {
            if (bfReader != null) {
                bfReader.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (tempPORfile.exists()) {
            tempPORfile.delete();
        }
    }

    dbgLog.fine("done parsing headers and decoding;");

    List<DataVariable> variableList = new ArrayList<>();

    for (int indx = 0; indx < variableTypelList.size(); indx++) {

        DataVariable dv = new DataVariable();
        String varName = variableNameList.get(indx);
        dv.setName(varName);
        String varLabel = variableLabelMap.get(varName);
        if (varLabel != null && varLabel.length() > 255) {
            varLabel = varLabel.substring(0, 255);
        }
        // TODO: do we still need to enforce the 255 byte limit on 
        // labels? is that enough to store whatever they have 
        // in their POR files at ODUM?
        // -- L.A. 4.0, beta11
        if (extendedLabels != null && extendedLabels.get(varName) != null) {
            dv.setLabel(extendedLabels.get(varName));
        } else {
            dv.setLabel(varLabel);
        }

        dv.setInvalidRanges(new ArrayList<>());
        dv.setSummaryStatistics(new ArrayList<>());
        dv.setUnf("UNF:6:");
        dv.setCategories(new ArrayList<>());
        dv.setFileOrder(indx);
        dv.setDataTable(dataTable);

        variableList.add(dv);

        int simpleType = 0;
        if (variableTypelList.get(indx) != null) {
            simpleType = variableTypelList.get(indx);
        }

        if (simpleType <= 0) {
            // We need to make one last type adjustment:
            // Dates and Times will be stored as character values in the 
            // dataverse tab files; even though they are not typed as 
            // strings at this point:
            // TODO: 
            // Make sure the date/time format is properly preserved!
            // (see the setFormatCategory below... but double-check!)
            // -- L.A. 4.0 alpha
            String variableFormatType = variableFormatTypeList[indx];

            if (variableFormatType != null) {
                if (variableFormatType.equals("time") || variableFormatType.equals("date")) {
                    simpleType = 1;

                    String formatCategory = formatCategoryTable.get(varName);

                    if (formatCategory != null) {
                        if (dateFormatList[indx] != null) {
                            dbgLog.fine("setting format category to " + formatCategory);
                            variableList.get(indx).setFormatCategory(formatCategory);
                            dbgLog.fine("setting formatschemaname to " + dateFormatList[indx]);
                            variableList.get(indx).setFormat(dateFormatList[indx]);
                        }
                    }
                } else if (variableFormatType.equals("other")) {
                    dbgLog.fine("Variable of format type \"other\"; type adjustment may be needed");
                    dbgLog.fine("SPSS print format: " + printFormatTable.get(variableList.get(indx).getName()));

                    if (printFormatTable.get(variableList.get(indx).getName()).equals("WKDAY")
                            || printFormatTable.get(variableList.get(indx).getName()).equals("MONTH")) {
                        // week day or month; 
                        // These are not treated as time/date values (meaning, we 
                        // don't define time/date formats for them; there's likely 
                        // no valid ISO time/date format for just a month or a day 
                        // of week). However, the
                        // values will be stored in the TAB files as strings, 
                        // and not as numerics - as they were stored in the 
                        // SAV file. So we need to adjust the type here.
                        // -- L.A. 

                        simpleType = 1;
                    }
                }
            }

        }

        dbgLog.fine("Finished creating variable " + indx + ", " + varName);

        // OK, we can now assign the types: 

        if (simpleType > 0) {
            // String: 
            variableList.get(indx).setTypeCharacter();
            variableList.get(indx).setIntervalDiscrete();
        } else {
            // Numeric: 
            variableList.get(indx).setTypeNumeric();
            // discrete or continuous?
            // "decimal variables" become dataverse data variables of interval type "continuous":

            if (decimalVariableSet.contains(indx)) {
                variableList.get(indx).setIntervalContinuous();
            } else {
                variableList.get(indx).setIntervalDiscrete();
            }

        }
        dbgLog.fine("Finished configuring variable type information.");
    }

    dbgLog.fine("done configuring variables;");

    /* 
     * From the original (3.6) code: 
    //smd.setVariableTypeMinimal(ArrayUtils.toPrimitive(variableTypelList.toArray(new Integer[variableTypelList.size()])));
    smd.setVariableFormat(printFormatList);
    smd.setVariableFormatName(printFormatNameTable);
    smd.setVariableFormatCategory(formatCategoryTable);
    smd.setValueLabelMappingTable(valueVariableMappingTable);
     * TODO: 
     * double-check that it's all being taken care of by the new plugin!
     * (for variable format and formatName, consult the SAV plugin)
     */

    dataTable.setDataVariables(variableList);

    // Assign value labels: 

    assignValueLabels(valueLabelTable);

    ingesteddata.setDataTable(dataTable);

    dbgLog.info("PORFileReader: read() end");
    return ingesteddata;
}