function urlbytes = urlget(urltext)
%
% Output is a numeric array of the values (unsigned: 0:1:255) of 
% retrieved bytes.
% Input is the string giving the URL from which to fetch the bytes.
%
%
% Adapted from matlab's 'urlread' and (hidden) 'urlreadwrite' commands:
% this function is limited only to 'GET' on plain 'http' URLs, and 
% ignores proxies;
% its redeeming feature is that is does not cock-up the data if that
% happens not to be text!
% See matlab's own functions to make your additions to this or 
% subtractions from those, if bothered about proxies, https etc.
%

if ~usejava('jvm')
    error('MATLAB:urlget:NoJvm','URLGET requires wretched "Java".');
end
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
try
    handler = sun.net.www.protocol.http.Handler;
    url = java.net.URL([],urltext,handler);
catch
    url = java.net.URL(urltext);
end
urlConnection = url.openConnection;
if isempty(urlConnection)
    warning('MATLAB:urlget:NoJvm', ...
        'urlconnection wasn''t properly opened: expect trouble');
end
urlbytes = [];
try
    inputStream = urlConnection.getInputStream;
    byteArrayOutputStream = java.io.ByteArrayOutputStream;
    isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
    isc.copyStream(inputStream, byteArrayOutputStream);
    inputStream.close;
    byteArrayOutputStream.close;
    urlbytes = typecast(byteArrayOutputStream.toByteArray', 'uint8');
catch
    warning('MATLAB:urlget:ConnectionFailed', 'Error downloading URL.');
end

