Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a downloadMedia function that returns an InputStream #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions java/org/webservice/fotolia/FotoliaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,49 @@ public void downloadMedia(final String download_url, final String output_file) t
if (output_file != null) {
stream.close();
}
}

/**
* Download a media and get the InputStream with its binary
*
* @param download_url URL as returned by getMedia()
*/
public InputStream getMediaInputStream(final String download_url) throws FileNotFoundException, IOException, FotoliaApiException
{
DefaultHttpClient client;
HttpResponse response;
StatusLine statusLine;
HttpEntity entity;
JSONObject obj;
InputStream inputStream = null;
String error_msg;
int error_code;

client = this._getHttpClient(true);
response = client.execute(new HttpGet(download_url));

statusLine = response.getStatusLine();
entity = response.getEntity();
if (statusLine.getStatusCode() != 200) {
if (entity == null) {
throw new FotoliaApiException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
} else {
obj = (JSONObject) JSONValue.parse(EntityUtils.toString(entity));
error_msg = (String) obj.get("error");
if (obj.get("code") != null) {
error_code = Integer.parseInt((String) obj.get("code"));
} else {
error_code = statusLine.getStatusCode();
}

throw new FotoliaApiException(error_code, error_msg);
}

}

inputStream = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
return inputStream;
}

/**
Expand Down