How can I connect to any site using NIO and read its data

Hi,

I am new to NIO and I need to know how could I connect to any web site using HTTP or FTP and read the site data by means of NIO. For instance this code

InetAddress ia = InetAddress.getByName("java.sun.com");

InetSocketAddress isa =new InetSocketAddress(ia,port);

server.socket().bind(isa);

makes the following error

Cannot assign requested address: bind

[459 byte] By [Isaaka] at [2007-11-26 23:46:10]
# 1
The operation you want here is connect(), not bind(). http://java.sun.com/j2se/1.5.0/docs/guide/nio/index.html
ejpa at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 2

OK, many thanks,

now it reads only tags when I want to download jpg

channel.write(ByteBuffer.wrap(request.getBytes()));

FileOutputStream fOut = null;

DataOutputStream out = null;

fOut=new FileOutputStream("C:\\temp\\yyyy.jpg");

out=new DataOutputStream(fOut);

int i=0;

int compl=0;

while ((i = channel.read(buffer)) != -1) {

buffer.flip();

compl += i;

out.write(i);

}

Isaaka at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 3

That just writes read-counts to the file. Not what you want! Open a FileChannel for output and do this:

buffer.clear();

while ((i = in.read(buffer)) > 0 || buffer.position() > 0)

{

buffer.flip();

fout.write(buffer);

buffer.compact();

}

ejpa at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 4

I am getting FIeNotFoundException here is the code

ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

socket = new ServerSocket(PORT);

FileInputStream input = new FileInputStream (new File(host));

channel = input.getChannel( );

fOut=new FileOutputStream("C:\\temp\\event_manager.gif");

WritableByteChannel dest = Channels.newChannel (fOut);

int i=0;

int compl=0;

buffer.clear();

while ( (i = channel.read(buffer)) > 0 || buffer.position() > 0) {

buffer.flip();

dest.write(buffer);

buffer.compact();

}

buffer.flip( );

} catch (UnknownHostException e) {

System.err.println(e);

} catch (IOException e) {

System.err.println(e);

} finally {

if (channel != null) {

try {

channel.close();

} catch (IOException ignored) {

}

}

}

and the exception is:

java.io.FileNotFoundException: http:\java.sun.com\developer\technicalArticles\Ecommerce\rfid\sjsrfid\event_manager.gif (The filename, directory name, or volume label syntax is incorrect)

Isaaka at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 5
No, that's not the code. Where's the part that tries to open http:\java.sun.com\developer\technicalArticles\Ecommerce\rfid\sjsrfid\event_manager.gif as a file?And what is the ServerSocket for?
ejpa at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 6

yes, you are right, here is the all code:

class ReadByteURL {

public static FileOutputStream fOut = null;

public static String encodedLogin = "";

private static ServerSocket socket;

public static FileChannel channel;

public static void main(String args[]) {

String host = "http://java.sun.com/developer/technicalArticles/Ecommerce/rfid/sjsrfid/event_manager.gif";

InetAddress sAddress;

final int PORT = 3128;

try {

URL _url = new URL(host);

HttpURLConnection urlConn = (HttpURLConnection)_url.openConnection();

urlConn.setRequestProperty("Proxy-Authorization",

"Basic " + encodedLogin);

urlConn.connect();

ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

FileInputStream input = new FileInputStream (new File(host));

channel = input.getChannel( );

fOut=new FileOutputStream("C:\\temp\\event_manager.gif");

WritableByteChannel dest = Channels.newChannel (fOut);

int i=0;

int compl=0;

buffer.clear();

while ( (i = channel.read(buffer)) > 0 || buffer.position() > 0) {

buffer.flip();

dest.write(buffer);

buffer.compact();

}

buffer.flip( );

} catch (UnknownHostException e) {

System.err.println(e);

} catch (IOException e) {

System.err.println(e);

} finally {

if (channel != null) {

try {

channel.close();

} catch (IOException ignored) {

}

}

}

}

}

Isaaka at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 7

Maybe you need to set followRedirects or some such URL/URLConnection/HttpURLConnection thing.

BTW what's the point of the final buffer.flip()? In fact what's the point of doing anything to an item that's about to go out of scope?

Also if you want to get any benefit from NIO you should use a much bigger buffer.

ejpa at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...
# 8

I don't know how much it's right but I did it this way and it works:

class ReadByteURL

{

public static FileOutputStream fOut = null;

public static String encodedLogin = "";

private static ServerSocket socket;

public static FileChannel channel;

public static void main(String args[])

{

String host = "http://www.any_pic.jpg";

InetAddress sAddress;

try {

manageProxy();

URL _url = new URL(host);

HttpURLConnection urlConn = (HttpURLConnection) _url.openConnection();

urlConn.setFollowRedirects(true);

urlConn.setRequestProperty("Proxy-Authorization",

"Basic " + encodedLogin);

urlConn.connect();

ByteBuffer buffer = ByteBuffer.allocateDirect(1024*8);

urlConn.setFollowRedirects(true);

System.out.println(" gggggggggg "+urlConn);

InputStream input = urlConn.getInputStream();

//

ReadableByteChannel rbc = Channels.newChannel(input);

// channel = input.getChannel( );

fOut=new FileOutputStream("C:\\temp\\event_manager.gif");

WritableByteChannel dest = Channels.newChannel (fOut);

int i=0;

int compl=0;

buffer.clear();

while ( (i = rbc.read(buffer)) > 0 || buffer.position() > 0) {

buffer.flip();

dest.write(buffer);

buffer.compact();

}

buffer.flip( );

} catch (UnknownHostException e) {

System.err.println(e);

} catch (IOException e) {

System.err.println(e);

} finally {

if (channel != null) {

try {

channel.close();

} catch (IOException ignored) {

}

}

}

}

public static void manageProxy() {

// Here goes proxy settings

}

}

ejp, many thanks to you for help.

Isaaka at 2007-7-11 15:19:07 > top of Java-index,Core,Core APIs...