Example usage for java.io Reader close

List of usage examples for java.io Reader close

Introduction

In this page you can find the example usage for java.io Reader close.

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:de.uzk.hki.da.metadata.EadMetsMetadataStructure.java

public EadMetsMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents)
        throws JDOMException, IOException, ParserConfigurationException, SAXException {
    super(workPath, metadataFile, documents);

    eadFile = metadataFile;/*from   w w  w.  ja  v  a  2s .  co  m*/

    SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder();
    FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, eadFile.getPath()));
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);
    Reader reader = new InputStreamReader(bomInputStream, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    eadDoc = builder.build(is);
    EAD_NS = eadDoc.getRootElement().getNamespace();
    eadParser = new EadParser(eadDoc);

    metsReferencesInEAD = eadParser.getReferences();
    metsFiles = getReferencedFiles(eadFile, metsReferencesInEAD, documents);

    mmsList = new ArrayList<MetsMetadataStructure>();
    for (File metsFile : metsFiles) {
        MetsMetadataStructure mms = new MetsMetadataStructure(workPath, metsFile, documents);
        mmsList.add(mms);
    }
    fileInputStream.close();
    bomInputStream.close();
    reader.close();
}

From source file:com.jitlogic.zico.core.UserManager.java

public synchronized void open() {

    if (db != null) {
        return;/*  ww w. j  a v  a2s  .  co m*/
    }

    db = dbf.openDB(ZorkaUtil.path(config.getConfDir(), "users.db"));
    users = db.getTreeMap("USERS");

    File jsonFile = new File(config.getConfDir(), "users.json");

    if (users.size() == 0 && jsonFile.exists()) {
        log.info("User DB is empty but JSON dump file found. Importing...");
        Reader reader = null;
        try {
            reader = new FileReader(jsonFile);
            JSONObject json = new JSONObject(new JSONTokener(reader));
            JSONArray names = json.names();
            for (int i = 0; i < names.length(); i++) {
                UserInfo user = fromJSON(json.getJSONObject(names.getString(i)));
                users.put(user.getUserName().toUpperCase(), user);
            }
            db.commit();
            log.info("User DB import finished successfully.");
        } catch (IOException e) {
            log.error("Cannot import user db from JSON data", e);
        } catch (JSONException e) {
            log.error("Cannot import user db from JSON data", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    if (userRealm != null) {
        for (UserInfo user : users.values()) {
            updateRealm(user.getUserName(), user.getPassword(), user.isAdmin());
        }

        if (sumode || users.size() == 0) {
            log.info(
                    "SU mode enabled or user database is empty. Adding default 'admin' user with 'zico' password.");
            updateRealm("admin", "zico", true);
        }
    } else {
        log.error(
                "Cannot initialize ZICO user realm. Logging in to collector will not be possible. Check jetty-web.xml for correctness.");
    }

}

From source file:com.seajas.search.contender.service.modifier.AbstractModifierService.java

/**
 * Create a StringBuffer out of the given reader content.
 * //from   w w  w.  j  av  a  2s .c o  m
 * @param buffer
 * @param reader
 * @param cleanFromProlog
 * @return Reader
 */
protected Reader readerToBuffer(final StringBuffer buffer, final Reader reader, final boolean cleanFromProlog) {
    Integer lastProlog = -1;

    try {
        char[] characterBuffer = new char[8192];

        for (int count; (count = reader.read(characterBuffer)) > 0;) {
            buffer.append(characterBuffer, 0, count);

            // When cleaning by prolog, we stop writing out the buffer after the second prolog

            if (cleanFromProlog) {
                if (lastProlog == -1)
                    lastProlog = buffer.indexOf("<?xml");
                if (lastProlog != -1 && (lastProlog = buffer.indexOf("<?xml", lastProlog + 1)) != -1) {
                    logger.warn("Input document contains more than one XML prolog. Stripping any extra ones.");

                    buffer.replace(lastProlog, buffer.length(), "");

                    break;
                }
            }
        }

        reader.close();

        return new StringReader(buffer.toString().trim());
    } catch (IOException e) {
        logger.error("Could not convert the given reader to a buffer", e);

        return null;
    }
}

From source file:com.jitlogic.zico.core.TraceTemplateManager.java

public void open() {

    if (db != null) {
        return;/*from ww w . ja v  a  2  s.com*/
    }

    db = dbf.openDB(ZorkaUtil.path(config.getConfDir(), "templates.db"));
    templates = db.getTreeMap("TEMPLATES");

    File jsonFile = new File(config.getConfDir(), "templates.json");

    if (templates.size() == 0 && jsonFile.exists()) {
        log.info("Templates DB is empty but JSON dump was found. Importing...");
        Reader reader = null;
        try {
            reader = new FileReader(jsonFile);
            JSONObject json = new JSONObject(new JSONTokener(reader));
            JSONArray names = json.names();
            for (int i = 0; i < names.length(); i++) {
                TraceTemplateInfo t = fromJSON(json.getJSONObject(names.getString(i)));
                templates.put(t.getId(), t);
            }
            db.commit();
            log.info("Template DB import finished succesfully.");
        } catch (IOException e) {
            log.error("Cannot import user db from JSON data", e);
        } catch (JSONException e) {
            log.error("Cannot import user db from JSON data", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    reorder();
}

From source file:com.google.acre.script.AcreFetch.java

@SuppressWarnings("boxing")
public void fetch(boolean system, String response_encoding, boolean log_to_user, boolean no_redirect) {

    if (request_url.length() > 2047) {
        throw new AcreURLFetchException("fetching URL failed - url is too long");
    }/*ww  w  .  j  a  v a  2 s.c o m*/

    DefaultHttpClient client = new DefaultHttpClient(_connectionManager, null);

    HttpParams params = client.getParams();

    // pass the deadline down to the invoked service.
    // this will be ignored unless we are fetching from another
    // acre server.
    // note that we may send a deadline that is already passed:
    // it's not our job to throw here since we don't know how
    // the target service will interpret the quota header.
    // NOTE: this is done *after* the user sets the headers to overwrite
    // whatever settings they might have tried to change for this value
    // (which could be a security hazard)
    long sub_deadline = (HostEnv.LIMIT_EXECUTION_TIME) ? _deadline - HostEnv.SUBREQUEST_DEADLINE_ADVANCE
            : System.currentTimeMillis() + HostEnv.ACRE_URLFETCH_TIMEOUT;
    int reentries = _reentries + 1;
    request_headers.put(HostEnv.ACRE_QUOTAS_HEADER, "td=" + sub_deadline + ",r=" + reentries);

    // if this is not an internal call, we need to invoke the call thru a proxy
    if (!_internal) {
        // XXX No sense wasting the resources to gzip inside the network.
        // XXX seems that twitter gets upset when we do this
        /*
        if (!request_headers.containsKey("accept-encoding")) {
        request_headers.put("accept-encoding", "gzip");
        }
        */
        String proxy_host = Configuration.Values.HTTP_PROXY_HOST.getValue();
        int proxy_port = -1;
        if (!(proxy_host.length() == 0)) {
            proxy_port = Configuration.Values.HTTP_PROXY_PORT.getInteger();
            HttpHost proxy = new HttpHost(proxy_host, proxy_port, "http");
            params.setParameter(AllClientPNames.DEFAULT_PROXY, proxy);
        }
    }

    params.setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    // in msec

    long timeout = _deadline - System.currentTimeMillis();
    if (timeout < 0)
        timeout = 0;
    params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, (int) timeout);
    params.setParameter(AllClientPNames.SO_TIMEOUT, (int) timeout);

    // we're not streaming the request so this should be a win.
    params.setParameter(AllClientPNames.TCP_NODELAY, true);

    // reuse an existing socket if it is in TIME_WAIT state.
    params.setParameter(AllClientPNames.SO_REUSEADDR, true);

    // set the encoding of our POST payloads to UTF-8
    params.setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, "UTF-8");

    BasicCookieStore cstore = new BasicCookieStore();
    for (AcreCookie cookie : request_cookies.values()) {
        cstore.addCookie(cookie.toClientCookie());
    }
    client.setCookieStore(cstore);

    HttpRequestBase method;

    HashMap<String, String> logmsg = new HashMap<String, String>();
    logmsg.put("Method", request_method);
    logmsg.put("URL", request_url);

    params.setParameter(AllClientPNames.HANDLE_REDIRECTS, !no_redirect);
    logmsg.put("Redirect", Boolean.toString(!no_redirect));

    try {
        if (request_method.equals("GET")) {
            method = new HttpGet(request_url);
        } else if (request_method.equals("POST")) {
            method = new HttpPost(request_url);
        } else if (request_method.equals("HEAD")) {
            method = new HttpHead(request_url);
        } else if (request_method.equals("PUT")) {
            method = new HttpPut(request_url);
        } else if (request_method.equals("DELETE")) {
            method = new HttpDelete(request_url);
        } else if (request_method.equals("PROPFIND")) {
            method = new HttpPropFind(request_url);
        } else {
            throw new AcreURLFetchException("Failed: unsupported (so far) method " + request_method);
        }
        method.getParams().setBooleanParameter(AllClientPNames.USE_EXPECT_CONTINUE, false);
    } catch (java.lang.IllegalArgumentException e) {
        throw new AcreURLFetchException("Unable to fetch URL; this is most likely an issue with URL encoding.");
    } catch (java.lang.IllegalStateException e) {
        throw new AcreURLFetchException("Unable to fetch URL; possibly an illegal protocol?");
    }

    StringBuilder request_header_log = new StringBuilder();
    for (Map.Entry<String, String> header : request_headers.entrySet()) {
        String key = header.getKey();
        String value = header.getValue();

        // XXX should suppress cookie headers?
        // content-type and length?

        if ("content-type".equalsIgnoreCase(key)) {
            Matcher m = contentTypeCharsetPattern.matcher(value);
            if (m.find()) {
                content_type = m.group(1);
                content_type_charset = m.group(2);
            } else {
                content_type_charset = "utf-8";
            }
            method.addHeader(key, value);
        } else if ("content-length".equalsIgnoreCase(key)) {
            // ignore user-supplied content-length, which is
            // probably wrong due to chars vs bytes and is
            // redundant anyway
            ArrayList<String> msg = new ArrayList<String>();
            msg.add("User-supplied content-length header is ignored");
            _acre_response.log("warn", msg);
        } else if ("user-agent".equalsIgnoreCase(key)) {
            params.setParameter(AllClientPNames.USER_AGENT, value);
        } else {
            method.addHeader(key, value);
        }
        if (!("x-acre-auth".equalsIgnoreCase(key))) {
            request_header_log.append(key + ": " + value + "\r\n");
        }
    }
    logmsg.put("Headers", request_header_log.toString());

    // XXX need more detailed error checking
    if (method instanceof HttpEntityEnclosingRequestBase && request_body != null) {

        HttpEntityEnclosingRequestBase em = (HttpEntityEnclosingRequestBase) method;
        try {
            if (request_body instanceof String) {
                StringEntity ent = new StringEntity((String) request_body, content_type_charset);
                em.setEntity(ent);
            } else if (request_body instanceof JSBinary) {
                ByteArrayEntity ent = new ByteArrayEntity(((JSBinary) request_body).get_data());
                em.setEntity(ent);
            }
        } catch (UnsupportedEncodingException e) {
            throw new AcreURLFetchException(
                    "Failed to fetch URL. " + " - Unsupported charset: " + content_type_charset);
        }
    }

    if (!system && log_to_user) {
        ArrayList<Object> msg = new ArrayList<Object>();
        msg.add("urlfetch request");
        msg.add(logmsg);
        _acre_response.log("debug", msg);
    }
    _logger.info("urlfetch.request", logmsg);

    long startTime = System.currentTimeMillis();

    try {
        // this sends the http request and waits
        HttpResponse hres = client.execute(method);
        status = hres.getStatusLine().getStatusCode();
        HashMap<String, String> res_logmsg = new HashMap<String, String>();
        res_logmsg.put("URL", request_url);
        res_logmsg.put("Status", ((Integer) status).toString());

        Header content_type_header = null;

        // translate response headers
        StringBuilder response_header_log = new StringBuilder();
        Header[] rawheaders = hres.getAllHeaders();
        for (Header rawheader : rawheaders) {
            String headername = rawheader.getName().toLowerCase();
            if (headername.equalsIgnoreCase("content-type")) {
                content_type_header = rawheader;
                // XXX should strip everything after ;
                content_type = rawheader.getValue();

                // XXX don't set content_type_parameters, deprecated?
            } else if (headername.equalsIgnoreCase("x-metaweb-cost")) {
                _costCollector.merge(rawheader.getValue());
            } else if (headername.equalsIgnoreCase("x-metaweb-tid")) {
                res_logmsg.put("ITID", rawheader.getValue());
            }

            headers.put(headername, rawheader.getValue());
            response_header_log.append(headername + ": " + rawheader.getValue() + "\r\n");
        }

        res_logmsg.put("Headers", response_header_log.toString());

        if (!system && log_to_user) {
            ArrayList<Object> msg = new ArrayList<Object>();
            msg.add("urlfetch response");
            msg.add(res_logmsg);
            _acre_response.log("debug", msg);
        }

        _logger.info("urlfetch.response", res_logmsg);

        // read cookies
        for (Cookie c : cstore.getCookies()) {
            cookies.put(c.getName(), new AcreCookie(c));
        }

        // get body encoding

        String charset = null;
        if (content_type_header != null) {
            HeaderElement values[] = content_type_header.getElements();
            if (values.length == 1) {
                NameValuePair param = values[0].getParameterByName("charset");
                if (param != null) {
                    charset = param.getValue();
                }
            }
        }

        if (charset == null)
            charset = response_encoding;

        // read body
        HttpEntity ent = hres.getEntity();
        if (ent != null) {
            InputStream res_stream = ent.getContent();
            Header cenc = ent.getContentEncoding();
            if (cenc != null && res_stream != null) {
                HeaderElement[] codecs = cenc.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        res_stream = new GZIPInputStream(res_stream);
                    }
                }
            }

            long firstByteTime = 0;
            long endTime = 0;
            if (content_type != null
                    && (content_type.startsWith("image/") || content_type.startsWith("application/octet-stream")
                            || content_type.startsWith("multipart/form-data"))) {
                // HttpClient's InputStream doesn't support mark/reset, so
                // wrap it with one that does.
                BufferedInputStream bufis = new BufferedInputStream(res_stream);
                bufis.mark(2);
                bufis.read();
                firstByteTime = System.currentTimeMillis();
                bufis.reset();
                byte[] data = IOUtils.toByteArray(bufis);

                endTime = System.currentTimeMillis();
                body = new JSBinary();
                ((JSBinary) body).set_data(data);

                try {
                    if (res_stream != null) {
                        res_stream.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            } else if (res_stream == null || charset == null) {
                firstByteTime = endTime = System.currentTimeMillis();
                body = "";
            } else {
                StringWriter writer = new StringWriter();
                Reader reader = new InputStreamReader(res_stream, charset);
                int i = reader.read();
                firstByteTime = System.currentTimeMillis();
                writer.write(i);
                IOUtils.copy(reader, writer);
                endTime = System.currentTimeMillis();
                body = writer.toString();

                try {
                    reader.close();
                    writer.close();
                } catch (IOException e) {
                    // ignore
                }
            }

            long waitingTime = firstByteTime - startTime;
            long readingTime = endTime - firstByteTime;

            _logger.debug("urlfetch.timings", "waiting time: " + waitingTime + "ms");
            _logger.debug("urlfetch.timings", "reading time: " + readingTime + "ms");

            Statistics.instance().collectUrlfetchTime(startTime, firstByteTime, endTime);

            _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw", waitingTime)
                    .collect((system) ? "asub" : "auub", waitingTime);
        }
    } catch (IllegalArgumentException e) {
        Throwable cause = e.getCause();
        if (cause == null)
            cause = e;
        throw new AcreURLFetchException("failed to fetch URL. " + " - Request Error: " + cause.getMessage());
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause == null)
            cause = e;
        throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage());
    } catch (RuntimeException e) {
        Throwable cause = e.getCause();
        if (cause == null)
            cause = e;
        throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage());
    } finally {
        method.abort();
    }
}

From source file:me.xiaopan.android.gohttp.StringHttpResponseHandler.java

private String toString(HttpRequest httpRequest, final HttpEntity entity, final String defaultCharset)
        throws IOException, ParseException {
    InputStream inputStream = entity.getContent();
    if (inputStream == null) {
        return "";
    }/*from ww w.  j  a  v  a  2 s.c om*/
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    int contentLength = (int) entity.getContentLength();
    if (contentLength < 0) {
        contentLength = 4096;
    }
    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    long averageLength = contentLength / httpRequest.getProgressCallbackNumber();
    int callbackNumber = 0;
    Reader reader = new InputStreamReader(inputStream, charset);
    CharArrayBuffer buffer = new CharArrayBuffer(contentLength);
    HttpRequest.ProgressListener progressListener = httpRequest.getProgressListener();
    try {
        char[] tmp = new char[1024];
        int readLength;
        long completedLength = 0;
        while (!httpRequest.isStopReadData() && (readLength = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, readLength);
            completedLength += readLength;
            if (progressListener != null && !httpRequest.isCanceled()
                    && (completedLength >= (callbackNumber + 1) * averageLength
                            || completedLength == contentLength)) {
                callbackNumber++;
                new HttpRequestHandler.UpdateProgressRunnable(httpRequest, contentLength, completedLength)
                        .execute();
            }
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:cn.dreampie.CoffeeCompiler.java

private void init() throws IOException {
    Reader reader = new InputStreamReader(coffeeJs.openStream(), "UTF-8");
    try {/*from  w w w .  j ava2  s .co m*/
        Context context = Context.enter();
        context.setOptimizationLevel(-1); // Without this, Rhino hits a 64K bytecode limit and fails
        try {
            globalScope = context.initStandardObjects();
            globalScope.put("logger", globalScope, Context.toObject(logger, globalScope));
            context.evaluateReader(globalScope, reader, "coffee-script.js", 0, null);
        } finally {
            Context.exit();
        }
    } catch (Exception e) {
        String message = "Failed to initialize Coffee compiler.";
        logger.error(message, e);
        throw new IllegalStateException(message, e);
    } finally {
        reader.close();
    }
}

From source file:com.jeeframework.util.xml.XMLProperties.java

/**
 * Builds the document XML model up based the given reader of XML data.
 *
 * @param in the input stream used to build the xml document
 * @throws IOException thrown when an error occurs reading the input stream.
 *///from   w w w . ja  v a2 s . co  m
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        Log.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.denimgroup.threadfix.framework.filefilter.ClassAnnotationBasedFileFilter.java

@Override
public boolean accept(@Nullable File file) {
    boolean returnValue = false;
    boolean hasArroba = false;

    if (file != null && file.exists() && file.isFile() && file.getName().endsWith(".java")) {
        Reader reader = null;

        try {//from ww w. j av a2  s.c o m

            reader = new InputStreamReader(new FileInputStream(file), "UTF-8");

            StreamTokenizer tokenizer = new StreamTokenizer(reader);
            tokenizer.slashSlashComments(true);
            tokenizer.slashStarComments(true);

            while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if (hasArroba && tokenizer.sval != null && getClassAnnotations().contains(tokenizer.sval)) {
                    returnValue = true;
                    break;
                } else if (tokenizer.sval != null && tokenizer.sval.equals("class")) {
                    // we've gone too far
                    break;
                }

                hasArroba = tokenizer.ttype == '@';
            }
        } catch (IOException e) {
            log.warn("Encountered IOException while tokenizing file.", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("Encountered IOException while attempting to close file.", e);
                }
            }
        }
    }

    return returnValue;
}

From source file:mixi4j.internal.http.HttpResponse.java

/**
 * Returns the response body as twitter4j.internal.org.json.JSONArray.<br>
 * Disconnects the internal HttpURLConnection silently.
 *
 * @return response body as twitter4j.internal.org.json.JSONArray
 * @throws MixiException//from  ww  w  .j a  v a2 s .c  o m
 */
public JSONArray asJSONArray() throws MixiException {
    if (jsonArray == null) {
        Reader reader = null;
        try {
            if (responseAsString == null) {
                reader = asReader();
                jsonArray = new JSONArray(new JSONTokener(reader));
            } else {
                jsonArray = new JSONArray(responseAsString);
            }
            if (CONF.isPrettyDebugEnabled()) {
                //                    logger.debug(jsonArray.toString(1));
            }
        } catch (JSONException jsone) {
            //                if (logger.isDebugEnabled()) {
            //                    throw new MixiException(jsone.getMessage() + ":" + this.responseAsString, jsone);
            //                } else {
            throw new MixiException(jsone.getMessage(), jsone);
            //                }
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignore) {
                }
            }
            disconnectForcibly();
        }
    }
    return jsonArray;
}