| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 import org.adblockplus.libadblockplus.ServerResponse; | 27 import org.adblockplus.libadblockplus.ServerResponse; |
| 28 import org.adblockplus.libadblockplus.WebRequest; | 28 import org.adblockplus.libadblockplus.WebRequest; |
| 29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
| 30 | 30 |
| 31 import android.util.Log; | 31 import android.util.Log; |
| 32 | 32 |
| 33 public class AndroidWebRequest extends WebRequest | 33 public class AndroidWebRequest extends WebRequest |
| 34 { | 34 { |
| 35 public final String TAG = Utils.getTag(WebRequest.class); | 35 public final String TAG = Utils.getTag(WebRequest.class); |
| 36 | 36 |
| 37 private static final int INITIAL_BUFFER_SIZE = 65536; |
| 38 private static final int BUFFER_GROWTH_DELTA = 65536; |
| 39 |
| 37 @Override | 40 @Override |
| 38 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) | 41 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) |
| 39 { | 42 { |
| 40 try | 43 try |
| 41 { | 44 { |
| 42 final URL url = new URL(urlStr); | 45 final URL url = new URL(urlStr); |
| 43 Log.d(this.TAG, "Downloading from: " + url); | 46 Log.d(this.TAG, "Downloading from: " + url); |
| 44 | 47 |
| 45 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); | 48 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
| 46 connection.setRequestMethod("GET"); | 49 connection.setRequestMethod("GET"); |
| 47 connection.connect(); | 50 connection.connect(); |
| 48 | 51 |
| 49 final ServerResponse response = new ServerResponse(); | 52 final ServerResponse response = new ServerResponse(); |
| 50 response.setResponseStatus(connection.getResponseCode()); | 53 response.setResponseStatus(connection.getResponseCode()); |
| 51 | 54 |
| 52 if (response.getResponseStatus() == 200) | 55 if (response.getResponseStatus() == 200) |
| 53 { | 56 { |
| 54 final InputStream in = connection.getInputStream(); | 57 final InputStream in = connection.getInputStream(); |
| 55 | 58 |
| 56 final byte[] buffer = new byte[4096]; | 59 final byte[] buffer = new byte[4096]; |
| 57 | 60 |
| 58 byte[] out = new byte[65536]; | 61 byte[] out = new byte[INITIAL_BUFFER_SIZE]; |
| 59 | 62 |
| 60 int pos = 0; | 63 int pos = 0; |
| 61 for (;;) | 64 for (;;) |
| 62 { | 65 { |
| 63 final int read = in.read(buffer); | 66 final int read = in.read(buffer); |
| 64 if (read < 0) | 67 if (read < 0) |
| 65 { | 68 { |
| 66 break; | 69 break; |
| 67 } | 70 } |
| 68 if (pos + read > out.length) | 71 if (pos + read > out.length) |
| 69 { | 72 { |
| 70 final byte[] old = out; | 73 final byte[] old = out; |
| 71 out = new byte[out.length + 65536]; | 74 out = new byte[out.length + BUFFER_GROWTH_DELTA]; |
| 72 System.arraycopy(old, 0, out, 0, pos); | 75 System.arraycopy(old, 0, out, 0, pos); |
| 73 } | 76 } |
| 74 System.arraycopy(buffer, 0, out, pos, read); | 77 System.arraycopy(buffer, 0, out, pos, read); |
| 75 pos += read; | 78 pos += read; |
| 76 } | 79 } |
| 77 | 80 |
| 78 connection.disconnect(); | 81 connection.disconnect(); |
| 79 | 82 |
| 80 response.setStatus(NsStatus.OK); | 83 response.setStatus(NsStatus.OK); |
| 81 response.setResponse(new String(out, 0, pos, "utf-8")); | 84 response.setResponse(new String(out, 0, pos, "utf-8")); |
| 82 } | 85 } |
| 83 else | 86 else |
| 84 { | 87 { |
| 85 response.setStatus(NsStatus.ERROR_FAILURE); | 88 response.setStatus(NsStatus.ERROR_FAILURE); |
| 86 } | 89 } |
| 87 return response; | 90 return response; |
| 88 } | 91 } |
| 89 catch (final Throwable t) | 92 catch (final Throwable t) |
| 90 { | 93 { |
| 91 throw new AdblockPlusException("WebRequest failed", t); | 94 throw new AdblockPlusException("WebRequest failed", t); |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 } | 97 } |
| LEFT | RIGHT |