package impl;
import api.Connection;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ConnectionImpl implements Connection {
URL url;
public ConnectionImpl(String urlString) {
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Override
public byte[] read(int startPos, int endPos) throws IOException {
ByteArrayOutputStream baos = null;
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
InputStream in = conn.getInputStream();
baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
in.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
@Override
public int getContentLength() {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
return conn.getContentLength();
} catch (IOException e) {
return -1;
} finally {
conn.disconnect();
}
}
@Override
public void close() {
}
}