Example usage for java.net URLConnection getContentLength

List of usage examples for java.net URLConnection getContentLength

Introduction

In this page you can find the example usage for java.net URLConnection getContentLength.

Prototype

public int getContentLength() 

Source Link

Document

Returns the value of the content-length header field.

Usage

From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4jZippedInstaller.java

public Neo4jZippedInstaller() {
    Thread checkSizeThread = new Thread() {
        @Override//from  w ww.  ja v  a2s . co  m
        public void run() {
            try {
                URLConnection connection = getUrl().openConnection();
                connection.setConnectTimeout(1000);
                connection.setReadTimeout(1000);
                size = connection.getContentLength();
            } catch (Exception e) {
                size = ERROR;
            }
            notifyListeners();
        }
    };
    checkSizeThread.start();
}

From source file:com.nextgis.mobile.util.ApkDownloader.java

@Override
protected String doInBackground(String... params) {
    try {/*www . j  av a 2  s.  co  m*/
        URL url = new URL(params[0]);
        mApkPath = Environment.getExternalStorageDirectory() + "/download/"
                + Uri.parse(params[0]).getLastPathSegment();
        URLConnection connection = url.openConnection();
        if (url.getProtocol().equalsIgnoreCase("https"))
            connection.setRequestProperty("Authorization", params[1]);

        connection.connect();
        int fileLength = connection.getContentLength() / 1024;
        InputStream input = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream(mApkPath);

        byte data[] = new byte[1024];
        long total = 0;
        int count;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int) total / 1024, fileLength);
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();

        return null;
    } catch (MalformedURLException e) {
        return mActivity.getString(R.string.error_invalid_url);
    } catch (IOException e) {
        return mActivity.getString(R.string.error_network_unavailable);
    }
}

From source file:org.ofbiz.content.data.DataResourceWorker.java

/**
 * getDataResourceStream - gets an InputStream and Content-Length of a DataResource
 *
 * @param dataResource/*from  ww  w.  j  ava2  s .  com*/
 * @param https
 * @param webSiteId
 * @param locale
 * @param contextRoot
 * @return Map containing 'stream': the InputStream and 'length' a Long containing the content-length
 * @throws IOException
 * @throws GeneralException
 */
public static Map<String, Object> getDataResourceStream(GenericValue dataResource, String https,
        String webSiteId, Locale locale, String contextRoot, boolean cache)
        throws IOException, GeneralException {
    if (dataResource == null) {
        throw new GeneralException("Cannot stream null data resource!");
    }

    String dataResourceTypeId = dataResource.getString("dataResourceTypeId");
    String dataResourceId = dataResource.getString("dataResourceId");
    Delegator delegator = dataResource.getDelegator();

    // first text based data
    if (dataResourceTypeId.endsWith("_TEXT") || "LINK".equals(dataResourceTypeId)) {
        String text = "";

        if ("SHORT_TEXT".equals(dataResourceTypeId) || "LINK".equals(dataResourceTypeId)) {
            text = dataResource.getString("objectInfo");
        } else if ("ELECTRONIC_TEXT".equals(dataResourceTypeId)) {
            GenericValue electronicText = EntityQuery.use(delegator).from("ElectronicText")
                    .where("dataResourceId", dataResourceId).cache(cache).queryOne();
            if (electronicText != null) {
                text = electronicText.getString("textData");
            }
        } else {
            throw new GeneralException("Unsupported TEXT type; cannot stream");
        }

        byte[] bytes = text.getBytes();
        return UtilMisc.toMap("stream", new ByteArrayInputStream(bytes), "length", Long.valueOf(bytes.length));

        // object (binary) data
    } else if (dataResourceTypeId.endsWith("_OBJECT")) {
        byte[] bytes = new byte[0];
        GenericValue valObj;

        if ("IMAGE_OBJECT".equals(dataResourceTypeId)) {
            valObj = EntityQuery.use(delegator).from("ImageDataResource")
                    .where("dataResourceId", dataResourceId).cache(cache).queryOne();
            if (valObj != null) {
                bytes = valObj.getBytes("imageData");
            }
        } else if ("VIDEO_OBJECT".equals(dataResourceTypeId)) {
            valObj = EntityQuery.use(delegator).from("VideoDataResource")
                    .where("dataResourceId", dataResourceId).cache(cache).queryOne();
            if (valObj != null) {
                bytes = valObj.getBytes("videoData");
            }
        } else if ("AUDIO_OBJECT".equals(dataResourceTypeId)) {
            valObj = EntityQuery.use(delegator).from("AudioDataResource")
                    .where("dataResourceId", dataResourceId).cache(cache).queryOne();
            if (valObj != null) {
                bytes = valObj.getBytes("audioData");
            }
        } else if ("OTHER_OBJECT".equals(dataResourceTypeId)) {
            valObj = EntityQuery.use(delegator).from("OtherDataResource")
                    .where("dataResourceId", dataResourceId).cache(cache).queryOne();
            if (valObj != null) {
                bytes = valObj.getBytes("dataResourceContent");
            }
        } else {
            throw new GeneralException("Unsupported OBJECT type [" + dataResourceTypeId + "]; cannot stream");
        }

        return UtilMisc.toMap("stream", new ByteArrayInputStream(bytes), "length", Long.valueOf(bytes.length));

        // file data
    } else if (dataResourceTypeId.endsWith("_FILE") || dataResourceTypeId.endsWith("_FILE_BIN")) {
        String objectInfo = dataResource.getString("objectInfo");
        if (UtilValidate.isNotEmpty(objectInfo)) {
            File file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, contextRoot);
            return UtilMisc.toMap("stream", new FileInputStream(file), "length", Long.valueOf(file.length()));
        } else {
            throw new GeneralException(
                    "No objectInfo found for FILE type [" + dataResourceTypeId + "]; cannot stream");
        }

        // URL resource data
    } else if ("URL_RESOURCE".equals(dataResourceTypeId)) {
        String objectInfo = dataResource.getString("objectInfo");
        if (UtilValidate.isNotEmpty(objectInfo)) {
            URL url = new URL(objectInfo);
            if (url.getHost() == null) { // is relative
                String newUrl = DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https);
                if (!newUrl.endsWith("/")) {
                    newUrl = newUrl + "/";
                }
                newUrl = newUrl + url.toString();
                url = new URL(newUrl);
            }

            URLConnection con = url.openConnection();
            return UtilMisc.toMap("stream", con.getInputStream(), "length",
                    Long.valueOf(con.getContentLength()));
        } else {
            throw new GeneralException("No objectInfo found for URL_RESOURCE type; cannot stream");
        }
    }

    // unsupported type
    throw new GeneralException(
            "The dataResourceTypeId [" + dataResourceTypeId + "] is not supported in getDataResourceStream");
}

From source file:MainClass.java

private byte[] loadClassData(String name) throws ClassNotFoundException {
    byte[] buffer;
    InputStream theClassInputStream = null;
    int bufferLength = 128;
    try {//www  .  j  a va2s  . c om
        URL classURL = new URL(url, name + ".class");
        URLConnection uc = classURL.openConnection();
        uc.setAllowUserInteraction(false);

        try {
            theClassInputStream = uc.getInputStream();
        } catch (NullPointerException e) {
            System.err.println(e);
            throw new ClassNotFoundException(name + " input stream problem");
        }
        int contentLength = uc.getContentLength();

        // A lot of web servers don't send content-lengths
        // for .class files
        if (contentLength == -1) {
            buffer = new byte[bufferLength * 16];
        } else {
            buffer = new byte[contentLength];
        }

        int bytesRead = 0;
        int offset = 0;

        while (bytesRead >= 0) {
            bytesRead = theClassInputStream.read(buffer, offset, bufferLength);
            if (bytesRead == -1)
                break;
            offset += bytesRead;
            if (contentLength == -1 && offset == buffer.length) { // grow the array
                byte temp[] = new byte[offset * 2];
                System.arraycopy(buffer, 0, temp, 0, offset);
                buffer = temp;
            } else if (offset > buffer.length) {
                throw new ClassNotFoundException(name + " error reading data into the array");
            }
        }

        if (offset < buffer.length) { // shrink the array
            byte temp[] = new byte[offset];
            System.arraycopy(buffer, 0, temp, 0, offset);
            buffer = temp;
        }

        // Make sure all the bytes were received
        if (contentLength != -1 && offset != contentLength) {
            throw new ClassNotFoundException("Only " + offset + " bytes received for " + name + "\n Expected "
                    + contentLength + " bytes");
        }
    } catch (Exception e) {
        throw new ClassNotFoundException(name + " " + e);
    } finally {
        try {
            if (theClassInputStream != null)
                theClassInputStream.close();
        } catch (IOException e) {
        }
    }
    return buffer;
}

From source file:org.danann.cernunnos.io.CopyFileTask.java

public void perform(TaskRequest req, TaskResponse res) {

    URL loc = resource.evaluate(req, res);
    String dir = to_dir != null ? (String) to_dir.evaluate(req, res) : null;
    String destination = (String) to_file.evaluate(req, res);

    InputStream is = null;/*from  ww  w.  j  av  a 2  s .  com*/
    OutputStream os = null;
    try {

        URLConnection conn = loc.openConnection();
        conn.connect();
        is = conn.getInputStream();

        if (log.isTraceEnabled()) {
            StringBuilder msg = new StringBuilder();
            msg.append("\n\turl=").append(loc.toString()).append("\n\tcontent-length=")
                    .append(conn.getContentLength());
            log.trace(msg);
        }

        File f = new File(dir, destination);
        if (f.getParentFile() != null) {
            // Make sure the necessary directories are in place...
            f.getParentFile().mkdirs();
        }
        os = new FileOutputStream(f);

        int bytesRead = 0;
        byte[] buf = new byte[4096];
        for (int len = is.read(buf); len > 0 || bytesRead < conn.getContentLength(); len = is.read(buf)) {
            os.write(buf, 0, len);
            bytesRead = bytesRead + len;
        }

    } catch (Throwable t) {
        String msg = "Unable to copy the specified file [" + loc.toExternalForm()
                + "] to the specified location [" + destination + "].";
        throw new RuntimeException(msg, t);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }

}

From source file:org.voyanttools.trombone.input.source.UriInputSource.java

/**
 * Create a new instance with the specified URI.
 * /*from   w  w  w.j a va 2  s .c  o  m*/
 * @param uri the URI associated with this input source
 * @throws IOException
 *             thrown when there's a problem creating or accessing header
 *             information for the URI
 * @throws MalformedURLException
 *             thrown if the URI is malformed
 */
public UriInputSource(URI uri) throws IOException {
    this.uri = uri;
    this.metadata = new DocumentMetadata();
    this.metadata.setLocation(uri.toString());

    this.metadata.setSource(Source.URI);

    String path = uri.getPath();
    if (path.isEmpty() || path.equals("/")) { // no path, use host
        metadata.setTitle(uri.getHost());
    } else if (path.endsWith("/")) { // ends in slash, use full path
        metadata.setTitle(path);
    } else { // try to use file part of URI
        metadata.setTitle(new File(path).getName());
    }

    StringBuilder idBuilder = new StringBuilder(uri.toString());

    // establish connection to find other and default metadata
    URLConnection c = null;
    try {
        c = getURLConnection(uri, 15000, 10000);

        // last modified of file
        long modified = c.getLastModified();
        this.metadata.setModified(modified);
        idBuilder.append(modified);

        // try and get length for id
        int length = c.getContentLength();
        idBuilder.append(length);

        String format = c.getContentType();
        if (format != null && format.isEmpty() == false) {
            idBuilder.append(format);
            DocumentFormat docFormat = DocumentFormat.fromContentType(format);
            if (docFormat != DocumentFormat.UNKNOWN) {
                this.metadata.setDefaultFormat(docFormat);
            }
        }

    } finally {
        if (c != null && c instanceof HttpURLConnection) {
            ((HttpURLConnection) c).disconnect();
        }
    }

    this.id = DigestUtils.md5Hex(idBuilder.toString());
}

From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4jZippedInstaller.java

@Override
public void performInstall(IProgressMonitor monitor, IPath dirPath) throws IOException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }/* w  ww .ja v a  2 s. c  om*/

    InputStream urlStream = null;
    try {
        URL url = getUrl();
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(TIMEOUT);
        connection.setReadTimeout(TIMEOUT);

        int length = connection.getContentLength();
        monitor.beginTask(NLS.bind("Reading from {1}", getVersion(), getUrl().toString()),
                length >= 0 ? length : IProgressMonitor.UNKNOWN);

        urlStream = connection.getInputStream();
        ZipInputStream zipStream = new ZipInputStream(urlStream);
        byte[] buffer = new byte[1024 * 8];
        long start = System.currentTimeMillis();
        int total = length;
        int totalRead = 0;
        ZipEntry entry;
        float kBps = -1;
        while ((entry = zipStream.getNextEntry()) != null) {
            if (monitor.isCanceled())
                break;
            String fullFilename = entry.getName();
            IPath fullFilenamePath = new Path(fullFilename);
            int secsRemaining = (int) ((total - totalRead) / 1024 / kBps);
            String textRemaining = secsToText(secsRemaining);
            monitor.subTask(NLS.bind("{0} remaining. Reading {1}",
                    textRemaining.length() > 0 ? textRemaining : "unknown time", StringUtils
                            .abbreviateMiddle(fullFilenamePath.removeFirstSegments(1).toString(), "...", 45)));
            int entrySize = (int) entry.getCompressedSize();
            OutputStream output = null;
            try {
                int len = 0;
                int read = 0;
                String action = null;
                if (jarFiles.contains(fullFilename)) {
                    action = "Copying";
                    String filename = FilenameUtils.getName(fullFilename);
                    output = new FileOutputStream(dirPath.append(filename).toOSString());
                } else {
                    action = "Skipping";
                    output = new NullOutputStream();
                }
                int secs = (int) ((System.currentTimeMillis() - start) / 1000);
                kBps = (float) totalRead / 1024 / secs;

                while ((len = zipStream.read(buffer)) > 0) {
                    if (monitor.isCanceled())
                        break;
                    read += len;
                    monitor.subTask(NLS.bind("{0} remaining. {1} {2} at {3}KB/s ({4}KB / {5}KB)",
                            new Object[] {
                                    String.format("%s",
                                            textRemaining.length() > 0 ? textRemaining : "unknown time"),
                                    action,
                                    StringUtils.abbreviateMiddle(
                                            fullFilenamePath.removeFirstSegments(1).toString(), "...", 45),
                                    String.format("%,.1f", kBps), String.format("%,.1f", (float) read / 1024),
                                    String.format("%,.1f", (float) entry.getSize() / 1024) }));
                    output.write(buffer, 0, len);
                }
                totalRead += entrySize;
                monitor.worked(entrySize);
            } finally {
                IOUtils.closeQuietly(output);
            }
        }
    } finally {
        IOUtils.closeQuietly(urlStream);
        monitor.done();
    }
}

From source file:de.indiplex.javapt.JavAPT.java

private void download(URLConnection con, File out) throws IOException {
    Finish = con.getContentLength();
    InputStream in = con.getInputStream();

    Util.checkFiles(out);/* ww w  . j  a  v  a 2s  .c o  m*/
    OutputStream fout = new FileOutputStream(out);
    int i = in.read();
    int r = 0;
    while (i != -1) {
        r++;
        fout.write(i);
        i = in.read();
        State = r;
    }
    fout.close();
}

From source file:org.massyframework.assembly.base.web.HttpResourceProcessorManagement.java

/**
 * ???Http?// w w w . ja v  a  2  s .c o m
 * @param req http
 * @param resp http?
 * @param resourcePath ?
 * @param resourceURL ?URL
 * @throws IOException ?IO
 */
private void writeResource(final HttpServletRequest req, final HttpServletResponse resp, final String pathInfo,
        final URL resourceURL) throws IOException {
    URLConnection connection = resourceURL.openConnection();
    long lastModified = connection.getLastModified();
    int contentLength = connection.getContentLength();

    String etag = null;
    if (lastModified != -1 && contentLength != -1)
        etag = "W/\"" + contentLength + "-" + lastModified + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

    // Check for cache revalidation.
    // We should prefer ETag validation as the guarantees are stronger and all HTTP 1.1 clients should be using it
    String ifNoneMatch = req.getHeader(IF_NONE_MATCH);
    if (ifNoneMatch != null && etag != null && ifNoneMatch.indexOf(etag) != -1) {
        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
    }

    long ifModifiedSince = req.getDateHeader(IF_MODIFIED_SINCE);
    // for purposes of comparison we add 999 to ifModifiedSince since the fidelity
    // of the IMS header generally doesn't include milli-seconds
    if (ifModifiedSince > -1 && lastModified > 0 && lastModified <= (ifModifiedSince + 999)) {
        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
    }

    // return the full contents regularly
    if (contentLength != -1)
        resp.setContentLength(contentLength);

    String contentType = getServletContext().getMimeType(pathInfo);
    if (contentType != null)
        resp.setContentType(contentType);

    if (lastModified > 0)
        resp.setDateHeader(LAST_MODIFIED, lastModified);

    if (etag != null)
        resp.setHeader(ETAG, etag);

    if (contentLength != 0) {
        // open the input stream
        InputStream is = null;
        try {
            is = connection.getInputStream();
            // write the resource
            try {
                OutputStream os = resp.getOutputStream();
                int writtenContentLength = writeResourceToOutputStream(is, os);
                if (contentLength == -1 || contentLength != writtenContentLength)
                    resp.setContentLength(writtenContentLength);
            } catch (IllegalStateException e) { // can occur if the response output is already open as a Writer
                Writer writer = resp.getWriter();
                writeResourceToWriter(is, writer);
                // Since ContentLength is a measure of the number of bytes contained in the body
                // of a message when we use a Writer we lose control of the exact byte count and
                // defer the problem to the Servlet Engine's Writer implementation.
            }
        } catch (FileNotFoundException e) {
            // FileNotFoundException may indicate the following scenarios
            // - url is a directory
            // - url is not accessible
            sendError(resp, HttpServletResponse.SC_FORBIDDEN);
        } catch (SecurityException e) {
            // SecurityException may indicate the following scenarios
            // - url is not accessible
            sendError(resp, HttpServletResponse.SC_FORBIDDEN);
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    // ignore
                }
        }
    }

}

From source file:org.pentaho.reporting.libraries.resourceloader.loader.URLResourceData.java

private void readMetaData(final URLConnection c) {
    modificationDate = c.getHeaderFieldDate("last-modified", -1);
    if (modificationDate <= 0) {
        if (isFixBrokenWebServiceDateHeader()) {
            modificationDate = System.currentTimeMillis();
        } else {//from   w  w w.j  a v a2s. c om
            modificationDate = -1;
        }
    }
    contentLength = new Long(c.getContentLength());
    contentType = c.getHeaderField("content-type");
    metaDataOK = true;
    lastDateMetaDataRead = System.currentTimeMillis();
}