james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sudhir Movva <sudhir.sw...@gmail.com>
Subject Partial Parse Input Stream to load Mime message.
Date Wed, 03 Jul 2013 06:06:57 GMT
Hi,
I am trying to use mime4j to read from inputStream only the first body part
into memory and stop reading (draining) from the input stream. I am using
this in a servlet request context. So, to be short and precise,

1. How can I partially read input stream

2. How to stitch the body part after some modification back to the input
stream and write the multipart to an output stream.


I used MultipartTokenStream to read from inputStream. I tried
MimeTokenStream.stop() when the first T_END_BODYPART event is raised but
observed that my input stream is empty(meaning even after calling the
stop(), the stream was completely drained?? correct me if my observation is
wrong)


MimeTokenStream stream = new MimeTokenStream();

stream.parseHeadless(is, contentType);

EntityState state = stream.getState();

while (state != EntityState.T_END_OF_STREAM) {

state = stream.next();

switch (state) {

    case T_START_MESSAGE:

System.out.println("Start Message");

        break;

  case T_END_MESSAGE:

System.out.println("End message");

break;

  case T_RAW_ENTITY:

System.out.println("T_RAW_ENTITY");

break;

  case T_START_HEADER:

System.out.println("T_START_HEADER");

break;

  case T_END_HEADER:

System.out.println("T_END_HEADER ");

break;

  case T_PREAMBLE:

System.out.println("T_PREAMBLE ");

break;

    case T_EPILOGUE:

  System.out.println("T_EPILOGUE ");

break;

  case T_START_BODYPART:

System.out.println("T_START_BODYPART ");

break;

  case T_END_OF_STREAM:

System.out.println("T_END_OF_STREAM ");

break;

  case T_BODY:

System.out.println("Inside T_BODY ");

IOUtils.copy(stream.getReader(), sw);

break;

  case T_END_BODYPART:

System.out.println("T_END_BODYPART ");

stream.stop();

        return sw.toString();

  case T_FIELD:

System.out.println("Header field detected: "

+ stream.getField());

break;

  case T_START_MULTIPART:

System.out.println("Multipart message detexted,"

+ " header data = " + stream.getBodyDescriptor());

break;


  case T_END_MULTIPART:

System.out.println("Multipart message end ");

break;

}
}

Thanks,
Sam

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message