So you mean that just using IoBuffer (in direct mode I suppose) will
do the job ? Great news !
But I'm not familiar with java buffers.
Time to read Java NIO from Ron Hitchens ...
So I'm trying to send a simple file to a server, and write it back to
a file.
The client side (that send the file) seems to work fine, but I don't
know how to implement the server side (that receive the stream, and
write it to fs using IoBuffer.
My code on the client side :
FileInputStream fis = new FileInputStream(path);
FileChannel fc = fis.getChannel();
ByteBuffer fb = ByteBuffer.allocate( 1024 * 15 );
fc.read(fb);
fb.flip();
IoBuffer fiob = IoBuffer.wrap(fb);
session.write(fiob);
Is it a good way to do this ?
You will create huge buffers. If your files are 2Gb big, it will suck up
all your memory. Just split the file in small chunks, it works exactly
the same way.
For the server, in the handler.messageReceived() function, I try to
cast the message to an IoBuffer, but it's probably not a good way. It
returns cast error from String (why ?) to IoBuffer.
You propably have a ProtocolCodec in the chain which does some kind of
transformation.
Be aware that the file will be received in chunks too, whatever you do.
In any case, *don't* store everything into a big IoBuffer before storing
the data in a file. Just open the file when you open the session, and
store the bytes as they arrive in the open file.
You will also have to define a minimal protocol : the first bytes you
will receive will probably be the file name, or something like that. The
ProtocolDecoder will have to handle this.
How can I do this ? A ByteBuffer that I wrap in a IoBuffer ? A piece
of code showing this would be greatly appreciated :)
IoBuffer alread wrap a ByteBuffer. You may have a loo, at the "Imagine"
sample : it's documented on the web site :
http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.html
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org