Bridges > Java Import Bridge > Accessing URLs Example

# Accessing URLs Example

This example finds and reads a given URL, which is contained in a file named URLReader.java. IDL then accesses this data through the URLRead routine, which is in a file named urlread.pro.

Note: These files are located in the resource/bridges/import/java/examples directory of the IDL distribution. Run this example procedure by entering urlread at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT urlread.pro.

The URLReader.java file contains the following text for reading a given URL in Java:

import java.io.*;

import java.net.*;

public class URLReader

{

private ByteArrayOutputStream m_buffer;

// ********************************************************

//

// Constructor. Create the reader

//

// ********************************************************

public URLReader()

{

m_buffer = new ByteArrayOutputStream();

}

// ********************************************************

//

// readURL: read the data from the URL into our buffer

//

// returns: number of bytes read (0 if invalid URL)

//

// NOTE: reading a new URL clears out the previous data

//

// ********************************************************

public int readURL(String sURL)

{

URL url;

InputStream in = null;

m_buffer.reset(); // reset our holding buffer to 0 bytes

int total_bytes = 0;

byte[] tempBuffer = new byte[4096];

try

{

url = new URL(sURL);

in = url.openStream();

int bytes_read;

while ((bytes_read = in.read(tempBuffer)) != -1)

{

m_buffer.write(tempBuffer, 0, bytes_read);

total_bytes += bytes_read;

}

}

catch (Exception e)

{

System.err.println("Error reading URL: "+sURL);

total_bytes = 0;

}

finally

{

try

{

in.close();

m_buffer.close();

}

catch (Exception e) {}

}

return total_bytes;

}

// ********************************************************

//

// getData: return the array of bytes

//

// ********************************************************

public byte[] getData()

{

return m_buffer.toByteArray();

}

// ********************************************************

//

// main: reads URL and reports # of bytes read

//

// Usage: java URLReader <URL>

//

// ********************************************************

public static void main(String[] args)

{

if (args.length != 1)

System.err.println("Usage: URLReader <URL>");

else

{

URLReader o = new URLReader();

int b = o.readURL(args[0]);

System.out.println("bytes="+b);

}

}

}

The urlread.pro file contains the following text for inputting an URL as an IDL string and then accessing its data within IDL:

FUNCTION URLRead, sURLName

; Create an URLReader.

oJURLReader = OBJ_NEW('IDLjavaObject$URLReader', 'URLReader')

; Read the URL data into our Java-side buffer.

nBytes = oJURLReader->ReadURL(sURLName)

;PRINT, 'Read ', nBytes, ' bytes'

; Pull the data into IDL.

byteArr = oJURLReader->GetData()

; Cleanup Java object.

OBJ_DESTROY, oJURLReader

; Return the data.

RETURN, byteArr

END

After saving and compiling the above files (URLReader.java in Java and urlread.pro in IDL), you can run the URLRead routine in IDL. This routine is a function with one input argument, which should be a IDL string containing an URL. For example:

address = 'http://www.ittvis.com'

data = URLRead(address)