List of usage examples for com.squareup.okhttp Headers names
public Set<String> names()
From source file:org.tinymediamanager.core.threading.DownloadTask.java
License:Apache License
@Override protected void doInBackground() { try {//from w w w . j a v a2 s . c o m // verify the url is not empty and starts with at least if (StringUtils.isBlank(url) || !url.toLowerCase().startsWith("http")) { return; } // if file extension is empty, detect from url, or content type String ext = FilenameUtils.getExtension(file.getName()); if (ext != null && ext.length() > 4) { ext = ""; // no extension when longer than 4 chars! } if (ext == null || ext.isEmpty()) { ext = UrlUtil.getExtension(url); if (!ext.isEmpty()) { if (Globals.settings.getAllSupportedFileTypes().contains("." + ext)) { file = new File(file.getParent(), file.getName() + "." + ext); } else { // unsupported filetype, eg php/asp/cgi script ext = ""; } } } LOGGER.info("Downloading " + url); StreamingUrl u = new StreamingUrl(UrlUtil.getURIEncoded(url).toASCIIString()); if (StringUtils.isNotBlank(userAgent)) { u.setUserAgent(userAgent); } InputStream is = u.getInputStream(); // trace server headers LOGGER.trace("Server returned: " + u.getStatusLine()); Headers headers = u.getHeadersResponse(); for (String name : headers.names()) { LOGGER.trace(" < " + name + ": " + headers.get(name)); } if (u.isFault()) { MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, u.getUrl(), u.getStatusLine())); is.close(); return; } long length = u.getContentLength(); String type = u.getContentEncoding(); if (ext.isEmpty()) { // still empty? try to parse from mime header if (type.startsWith("video/") || type.startsWith("audio/") || type.startsWith("image/")) { ext = type.split("/")[1]; ext = ext.replaceAll("x-", ""); // x-wmf and others file = new File(file.getParent(), file.getName() + "." + ext); } } LOGGER.info("Downloading to " + file); File tempFile = new File(file.getAbsolutePath() + ".part"); BufferedInputStream bufferedInputStream = new BufferedInputStream(is); FileOutputStream outputStream = new FileOutputStream(tempFile); int count = 0; byte buffer[] = new byte[2048]; Long timestamp1 = System.nanoTime(); Long timestamp2; long bytesDone = 0; long bytesDonePrevious = 0; double speed = 0; while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1) { if (cancel) { break; } outputStream.write(buffer, 0, count); bytesDone += count; // we push the progress only once per 250ms (to use less performance and get a better download speed) timestamp2 = System.nanoTime(); if (timestamp2 - timestamp1 > 250000000) { // avg. speed between the actual and the previous speed = (speed + (bytesDone - bytesDonePrevious) / ((double) (timestamp2 - timestamp1) / 1000000000)) / 2; timestamp1 = timestamp2; bytesDonePrevious = bytesDone; if (length > 0) { publishState(formatBytesForOutput(bytesDone) + "/" + formatBytesForOutput(length) + " @" + formatSpeedForOutput(speed), (int) (bytesDone * 100 / length)); } else { setWorkUnits(0); publishState(formatBytesForOutput(bytesDone) + " @" + formatSpeedForOutput(speed), 0); } } } outputStream.close(); // we must not close the input stream on cancel(the rest will be downloaded if we close it on cancel) if (!cancel) { is.close(); } if (cancel) { // delete half downloaded file Utils.deleteFileSafely(tempFile); } else { if (ext.isEmpty()) { // STILL empty? hmpf... // now we have a chicken-egg problem: // MediaInfo needs MF type to correctly fetch extension // to detect MF type, we need the extension // so we are forcing to read the container type direct on tempFile MediaFile mf = new MediaFile(tempFile); mf.setContainerFormatDirect(); // force direct read of mediainfo - regardless of filename!!! ext = mf.getContainerFormat(); if (!ext.isEmpty()) { file = new File(file.getParent(), file.getName() + "." + ext); } } Utils.deleteFileSafely(file); // delete existing file boolean ok = Utils.moveFileSafe(tempFile, file); if (ok) { Utils.deleteFileSafely(tempFile); if (media != null) { MediaFile mf = new MediaFile(file, fileType); mf.gatherMediaInformation(); media.removeFromMediaFiles(mf); // remove old (possibly same) file media.addToMediaFiles(mf); // add file, but maybe with other MI values media.saveToDb(); } } else { LOGGER.warn("Download to '" + tempFile + "' was ok, but couldn't move to '" + file + "'"); } } // end isCancelled } catch (Exception e) { LOGGER.error("problem downloading: ", e); } }
From source file:twitter4j.OkHttpResponse.java
License:Apache License
public OkHttpResponse(Response response, HttpClientConfiguration conf) throws IOException { super(conf);/*from ww w . j av a 2 s. c o m*/ this.response = response; Headers headers = response.headers(); Set<String> names = headers.names(); HashMap<String, List<String>> headerFields = new HashMap<String, List<String>>(); for (String name : names) { headerFields.put(name, headers.values(name)); } this.headerFields = headerFields; is = response.body().byteStream(); if (is != null && "gzip".equals(response.header("Content-Encoding"))) { is = new StreamingGZIPInputStream(is); } statusCode = response.code(); }