James,

I implemented a quick-and-dirty decoder that streams the images to disk
instead of reading all bytes to memory:

public class ImageStreamingDecoder extends CumulativeProtocolDecoder {
   private static final String DECODER_STATE_KEY =
ImageStreamingDecoder.class.getName() + ".STATE";

   private static class DecoderState {
       int imageNr = 0;
       boolean lengthRead = false;
       int totalLength;
       int bytesRead;
       File file;
       FileOutputStream outputStream;
   }

   protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
       DecoderState decoderState = (DecoderState) session.getAttribute
(DECODER_STATE_KEY);
       if (decoderState == null) {
           decoderState = new DecoderState();
           session.setAttribute(DECODER_STATE_KEY, decoderState);
       }
       if (decoderState.imageNr < 2) {
           if (!decoderState.lengthRead) {
               if (in.remaining() >= 4) {
                   decoderState.totalLength = in.getInt();
                   System.out.println("decoderState.totalLength = " +
decoderState.totalLength);
                   decoderState.bytesRead = 0;
                   decoderState.lengthRead = true;
                   decoderState.file = new File("/image" +
decoderState.imageNr + ".png");
                   decoderState.outputStream = new FileOutputStream(
decoderState.file);
               } else {
                   return false;
               }
           }
           int bytesToRead = Math.min(decoderState.totalLength -
decoderState.bytesRead, in.remaining());
           byte[] bytes = new byte[bytesToRead];
           in.get(bytes);
           System.out.println("bytes read: " + bytesToRead);
           decoderState.bytesRead += bytesToRead;
           decoderState.outputStream.write(bytes);
           if (decoderState.bytesRead == decoderState.totalLength) {
               SessionLog.info(session, "image " + decoderState.imageNr + "
complete");
               decoderState.outputStream.close();
               out.write(decoderState.file);
               decoderState.imageNr++;
               decoderState.lengthRead = false;
           }
       }
       if (decoderState.imageNr == 2) {
           SessionLog.info(session, "both images received");
           session.removeAttribute(DECODER_STATE_KEY);
       }
       return true;
   }

   public void dispose(IoSession session) throws Exception {
       super.dispose(session);
       DecoderState decoderState = (DecoderState) session.getAttribute
(DECODER_STATE_KEY);
       if (decoderState != null && decoderState.outputStream != null) {
           decoderState.outputStream.close();
       }
       session.removeAttribute(DECODER_STATE_KEY);
   }
}

Maarten

On 5/28/07, James Im <[EMAIL PROTECTED]> wrote:

Very good tutorial!

I've just seen a little typo in the remarks about the ImageResponse
Decoder. It says: "the first PICE of data is handled by thread-1".

While reading your tutorial I was wondering what you would do if the
image to be sent was very big, so big that it wouldn't fit in memory and
must be streamed to and from the hard disk.


>On 5/28/07, Maarten Bosteels <[EMAIL PROTECTED]> wrote:
>>Hello all,
>>
>>I created a tutorial on creating a custom ProtocolCodecFilter.
>>Please have a look at it and let me know what you think:
>>
>>http://cwiki.apache.org/confluence/display/MINA/Tutorial on
>>ProtocolCodecFilter

_________________________________________________________________
Opret en personlig blog og del dine billeder på MSN Spaces:
http://spaces.msn.com/


Reply via email to