(final String url)
| 264 | } |
| 265 | |
| 266 | public static byte[] fetch(final String url) throws IOException { |
| 267 | class BlockingReceiver implements Receiver { |
| 268 | |
| 269 | private boolean done = false; |
| 270 | private byte[] content = null; |
| 271 | private String errorMessage = null; |
| 272 | |
| 273 | @Override |
| 274 | public void onContentFetched(byte[] content) { |
| 275 | this.content = content; |
| 276 | this.done = true; |
| 277 | synchronized (this) { |
| 278 | this.notify(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void onError(byte[] description) { |
| 284 | this.errorMessage = new String(description); |
| 285 | this.done = true; |
| 286 | synchronized (this) { |
| 287 | this.notify(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | public byte[] getContent() { |
| 292 | return content; |
| 293 | } |
| 294 | |
| 295 | public boolean isDone() { |
| 296 | return done; |
| 297 | } |
| 298 | |
| 299 | public String getErrorMessage() { |
| 300 | return errorMessage; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | RemoteReader reader = fromUrl(url); |
| 305 | BlockingReceiver r = new BlockingReceiver(); |
| 306 | Task t = new Task(url, r); |
| 307 | reader.addFirst(t); |
| 308 | |
| 309 | try { |
| 310 | synchronized (r) { |
| 311 | while(!r.isDone()) |
| 312 | r.wait(); |
| 313 | } |
| 314 | } catch (InterruptedException e) { |
| 315 | throw new IOException("Interrupted while fetching content"); |
| 316 | } |
| 317 | |
| 318 | byte[] res = r.getContent(); |
| 319 | |
| 320 | if(res == null) |
| 321 | throw new IOException(r.getErrorMessage()); |
| 322 | |
| 323 | return res; |
no test coverage detected