Created
May 26, 2014 07:59
-
-
Save practice/334fb1d37e1f14b51651 to your computer and use it in GitHub Desktop.
GAE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private byte[] downloadFile(String downloadUrl) throws MalformedURLException, | |
IOException, ProtocolException { | |
URL url = new URL(downloadUrl); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
ReadableByteChannel src = Channels.newChannel(connection.getInputStream()); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
WritableByteChannel dest = Channels.newChannel(baos); | |
ChannelTools.fastChannelCopy(src, dest); | |
src.close(); | |
dest.close(); | |
int statusCode = connection.getResponseCode(); | |
if (statusCode != HttpURLConnection.HTTP_OK) { | |
String msg = String.format("Server returned with error: %s(%d)", HttpStatus.valueOf(statusCode).name(), statusCode); | |
log.error(msg); | |
throw new IOException(msg); | |
} | |
return baos.toByteArray(); | |
} | |
public class ChannelTools { | |
public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { | |
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); | |
while (src.read(buffer) != -1) { | |
// prepare the buffer to be drained | |
buffer.flip(); | |
// write to the channel, may block | |
dest.write(buffer); | |
// If partial transfer, shift remainder down | |
// If buffer is empty, same as doing clear() | |
buffer.compact(); | |
} | |
// EOF will leave buffer in fill state | |
buffer.flip(); | |
// make sure the buffer is fully drained. | |
while (buffer.hasRemaining()) { | |
dest.write(buffer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment