import jalview.datamodel.DBRefSource;
import jalview.util.MessageManager;
+import jalview.util.Platform;
-import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
* @param database
* @param format
* @param outFile
- * @return
+ * @return array of lines from EBI only if outFile is null (which it will not
+ * be)
* @throws OutOfMemoryError
*/
String[] fetchBatch(String ids, String database, String format,
File outFile) throws OutOfMemoryError
{
- // long time = System.currentTimeMillis();
String url = buildUrl(ids, database, format);
-
+ InputStream is = null;
try
{
URL rcall = new URL(url);
-
HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
int responseCode = conn.getResponseCode();
- if (responseCode != 200)
- {
- System.err.println("Warning: response code " + responseCode
- + " for " + url);
- }
- InputStream is = new BufferedInputStream(conn.getInputStream());
- if (outFile != null)
+ if (responseCode == 200)
{
- /**@j2sNative
- * if (outFile._isTemp) {
- * outFile._bytes = is.$in.$in.buf;
- * return null;
- * }
- */
- FileOutputStream fio = new FileOutputStream(outFile);
- byte[] bb = new byte[32 * 1024];
- int l;
- while ((l = is.read(bb)) > 0)
+ is = conn.getInputStream();
+ if (outFile != null)
{
- fio.write(bb, 0, l);
+ Platform.streamToFile(is, outFile);
+ return null;
}
- fio.close();
- is.close();
- }
- else
- {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String rtn;
- List<String> arl = new ArrayList<String>();
+ List<String> arl = new ArrayList<>();
while ((rtn = br.readLine()) != null)
{
arl.add(rtn);
}
- return arl.toArray(new String[arl.size()]);
+ return (String[]) arl.toArray();
}
+ System.err.println(
+ "Warning: response code " + responseCode + " for " + url);
} catch (OutOfMemoryError er)
{
System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
throw er;
} catch (Exception ex)
{
- if (ex.getMessage().startsWith(
+ if (!ex.getMessage().startsWith(
"uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
{
- return null;
+ System.err.println("Unexpected exception when retrieving from "
+ + database + "\nQuery was : '" + ids + "'");
+ ex.printStackTrace(System.err);
}
- System.err.println("Unexpected exception when retrieving from "
- + database + "\nQuery was : '" + ids + "'");
- ex.printStackTrace(System.err);
- return null;
} finally
{
- // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
- // time) + " ms");
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ } catch (IOException e)
+ {
+ }
+ }
}
return null;
}