3 |
// kopieret fra dk.thoerup.genericjavautils; |
// kopieret fra dk.thoerup.genericjavautils; |
4 |
|
|
5 |
import java.io.ByteArrayOutputStream; |
import java.io.ByteArrayOutputStream; |
6 |
|
import java.io.File; |
7 |
|
import java.io.FileOutputStream; |
8 |
import java.io.IOException; |
import java.io.IOException; |
9 |
import java.io.InputStream; |
import java.io.InputStream; |
10 |
import java.io.OutputStream; |
import java.io.OutputStream; |
123 |
return readInputStream(is); |
return readInputStream(is); |
124 |
} |
} |
125 |
|
|
126 |
|
public static void getContentToFile(String uri, File f, int timeout) throws IOException { |
127 |
|
URL url = new URL(uri); |
128 |
|
URLConnection connection = url.openConnection(); |
129 |
|
|
130 |
|
connection.setConnectTimeout(timeout); |
131 |
|
connection.setReadTimeout(timeout); |
132 |
|
InputStream is = connection.getInputStream(); |
133 |
|
|
134 |
|
saveInputStreamToFile(is,f); |
135 |
|
} |
136 |
|
|
137 |
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
138 |
URL url = new URL(uri); |
URL url = new URL(uri); |
139 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
151 |
} |
} |
152 |
|
|
153 |
static byte[] readInputStream(InputStream is) throws IOException{ |
static byte[] readInputStream(InputStream is) throws IOException{ |
154 |
byte buffer[] = new byte[1024]; |
byte buffer[] = new byte[64 * 1024]; |
155 |
//try /; //start buffer size - instead of default 32bytes |
//try /; //start buffer size - instead of default 32bytes |
156 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) { |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(16*1024*1024)) { |
157 |
int count; |
int count; |
158 |
|
|
159 |
while ( (count = is.read(buffer)) != -1) { |
while ( (count = is.read(buffer)) != -1) { |
164 |
} |
} |
165 |
} |
} |
166 |
|
|
167 |
|
static void saveInputStreamToFile(InputStream is,File fOut) throws IOException{ |
168 |
|
byte buffer[] = new byte[64 * 1024]; |
169 |
|
|
170 |
|
//try /; //start buffer size - instead of default 32bytes |
171 |
|
try (FileOutputStream fos = new FileOutputStream(fOut) ) { |
172 |
|
int count; |
173 |
|
|
174 |
|
while ( (count = is.read(buffer)) != -1) { |
175 |
|
|
176 |
|
fos.write(buffer, 0, count); |
177 |
|
} |
178 |
|
is.close(); |
179 |
|
} |
180 |
|
|
181 |
|
} |
182 |
|
|
183 |
} |
} |
184 |
|
|