* @param uniqueID
* a unique ID used to locate the cached resource
*
+ * @return the number of bytes written
+ *
* @throws IOException
* in case of I/O error
*/
- public void save(InputStream in, String uniqueID) throws IOException {
+ public long save(InputStream in, String uniqueID) throws IOException {
File cached = getCached(uniqueID);
cached.getParentFile().mkdirs();
- save(in, cached);
+ return save(in, cached);
}
/**
* @param url
* the {@link URL} used to locate the cached resource
*
+ * @return the number of bytes written
+ *
* @throws IOException
* in case of I/O error
*/
- public void save(InputStream in, URL url) throws IOException {
+ public long save(InputStream in, URL url) throws IOException {
File cached = getCached(url);
- save(in, cached);
+ return save(in, cached);
}
/**
* @param cached
* the cached {@link File} to save to
*
+ * @return the number of bytes written
+ *
* @throws IOException
* in case of I/O error
*/
- private void save(InputStream in, File cached) throws IOException {
+ private long save(InputStream in, File cached) throws IOException {
// We delete AFTER so not to remove the subdir we will use...
- IOUtils.write(in, cached);
+ long bytes = IOUtils.write(in, cached);
clean(true, dir, 10);
+ return bytes;
}
/**
package be.nikiroo.utils;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",
Integer.toString(requestData.length()));
- conn.setRequestProperty("charset", "utf-8");
}
if (oauth != null) {
conn.connect();
// Check if redirect
- // BEWARE! POST data cannot be redirected, so it is ignored here
+ // BEWARE! POST data cannot be redirected (some webservers complain) for
+ // HTTP codes 302 and 303
if (conn instanceof HttpURLConnection) {
int repCode = 0;
try {
if (repCode / 100 == 3) {
String newUrl = conn.getHeaderField("Location");
return open(new URL(newUrl), originalUrl, currentReferer,
- cookiesValues, null, getParams, oauth, stable);
+ cookiesValues, //
+ (repCode == 302 || repCode == 303) ? null : postParams, //
+ getParams, oauth, stable);
}
}
- InputStream in = conn.getInputStream();
- if ("gzip".equals(conn.getContentEncoding())) {
- in = new GZIPInputStream(in);
- }
+ try {
+ InputStream in = conn.getInputStream();
+ if ("gzip".equals(conn.getContentEncoding())) {
+ in = new GZIPInputStream(in);
+ }
- if (in != null && cache != null) {
- tracer.trace("Save to cache: " + originalUrl);
- try {
+ if (in == null) {
+ throw new IOException("No InputStream!");
+ }
+
+ if (cache != null) {
+ String size = conn.getContentLengthLong() < 0 ? "unknown size"
+ : StringUtils.formatNumber(conn.getContentLengthLong())
+ + "bytes";
+ tracer.trace("Save to cache (" + size + "): " + originalUrl);
try {
- cache.save(in, originalUrl);
- } finally {
- in.close();
+ try {
+ long bytes = cache.save(in, originalUrl);
+ tracer.trace("Saved to cache: "
+ + StringUtils.formatNumber(bytes) + "bytes");
+ } finally {
+ in.close();
+ }
+ in = cache.load(originalUrl, true, true);
+ } catch (IOException e) {
+ tracer.error(new IOException(
+ "Cannot save URL to cache, will ignore cache: "
+ + url, e));
}
- in = cache.load(originalUrl, true, false);
- } catch (IOException e) {
- tracer.error(new IOException(
- "Cannot save URL to cache, will ignore cache: " + url,
- e));
}
- }
- return in;
+ return in;
+ } catch (IOException e) {
+ throw new IOException(String.format(
+ "Cannot find %s (current URL: %s)", originalUrl, url), e);
+ }
}
/**
conn.setRequestProperty("User-Agent", UA);
conn.setRequestProperty("Accept-Encoding", "gzip");
conn.setRequestProperty("Accept", "*/*");
+ conn.setRequestProperty("Charset", "utf-8");
if (currentReferer != null) {
conn.setRequestProperty("Referer", currentReferer.toString());
* <p>
* Examples:
* <ul>
- * <li><tt>8 765</tt> becomes "8k"</li>
- * <li><tt>998 765</tt> becomes "998k"</li>
- * <li><tt>12 987 364</tt> becomes "12M"</li>
- * <li><tt>5 534 333 221</tt> becomes "5G"</li>
+ * <li><tt>8 765</tt> becomes "8 k"</li>
+ * <li><tt>998 765</tt> becomes "998 k"</li>
+ * <li><tt>12 987 364</tt> becomes "12 M"</li>
+ * <li><tt>5 534 333 221</tt> becomes "5 G"</li>
* </ul>
*
* @param value
* <p>
* Examples (assuming decimalPositions = 1):
* <ul>
- * <li><tt>8 765</tt> becomes "8.7k"</li>
- * <li><tt>998 765</tt> becomes "998.7k"</li>
- * <li><tt>12 987 364</tt> becomes "12.9M"</li>
- * <li><tt>5 534 333 221</tt> becomes "5.5G"</li>
+ * <li><tt>8 765</tt> becomes "8.7 k"</li>
+ * <li><tt>998 765</tt> becomes "998.7 k"</li>
+ * <li><tt>12 987 364</tt> becomes "12.9 M"</li>
+ * <li><tt>5 534 333 221</tt> becomes "5.5 G"</li>
* </ul>
*
* @param value
*/
public static String formatNumber(long value, int decimalPositions) {
long userValue = value;
- String suffix = "";
+ String suffix = " ";
long mult = 1;
if (value >= 1000000000l) {