2 * Copyright (C) 2017 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org
.strongswan
.android
.logic
;
18 import android
.support
.annotation
.Keep
;
20 import java
.io
.BufferedOutputStream
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.io
.OutputStream
;
25 import java
.net
.HttpURLConnection
;
29 public class SimpleFetcher
31 public static byte[] fetch(String uri
, byte[] data
, String contentType
) throws IOException
33 URL url
= new URL(uri
);
34 HttpURLConnection conn
= (HttpURLConnection
)url
.openConnection();
35 conn
.setConnectTimeout(10000);
36 conn
.setReadTimeout(10000);
39 if (contentType
!= null)
41 conn
.setRequestProperty("Content-Type", contentType
);
45 conn
.setDoOutput(true);
46 conn
.setFixedLengthStreamingMode(data
.length
);
47 OutputStream out
= new BufferedOutputStream(conn
.getOutputStream());
51 return streamToArray(conn
.getInputStream());
59 private static byte[] streamToArray(InputStream in
) throws IOException
61 ByteArrayOutputStream out
= new ByteArrayOutputStream();
62 byte[] buf
= new byte[1024];
67 while ((len
= in
.read(buf
)) != -1)
69 out
.write(buf
, 0, len
);
71 return out
.toByteArray();