Ejemplo n.º 1
0
 private void advanceToBoundary() throws IOException {
   if (!dataStream.eof()) {
     if (tmpbuf == null) {
       tmpbuf = new byte[2048];
     }
     InputStream instream = getLimitedContentStream();
     while (instream.read(tmpbuf) != -1) {}
   }
 }
Ejemplo n.º 2
0
  public EntityStateMachine advance() throws IOException, MimeException {
    switch (state) {
      case EntityStates.T_START_MESSAGE:
        state = EntityStates.T_START_HEADER;
        break;
      case EntityStates.T_START_BODYPART:
        state = EntityStates.T_START_HEADER;
        break;
      case EntityStates.T_START_HEADER:
      case EntityStates.T_FIELD:
        state = parseField() ? EntityStates.T_FIELD : EntityStates.T_END_HEADER;
        break;
      case EntityStates.T_END_HEADER:
        String mimeType = body.getMimeType();
        if (recursionMode == RecursionMode.M_FLAT) {
          state = EntityStates.T_BODY;
        } else if (MimeUtil.isMultipart(mimeType)) {
          state = EntityStates.T_START_MULTIPART;
          clearMimePartStream();
        } else if (recursionMode != RecursionMode.M_NO_RECURSE && MimeUtil.isMessage(mimeType)) {
          state = EntityStates.T_BODY;
          return nextMessage();
        } else {
          state = EntityStates.T_BODY;
        }
        break;
      case EntityStates.T_START_MULTIPART:
        if (dataStream.isUsed()) {
          advanceToBoundary();
          state = EntityStates.T_END_MULTIPART;
          break;
        } else {
          createMimePartStream();
          state = EntityStates.T_PREAMBLE;

          if (!currentMimePartStream.isEmptyStream()) break;
          // continue to next state
        }
      case EntityStates.T_PREAMBLE:
        // removed specific code. Fallback to T_IN_BODYPART that
        // better handle missing parts.
        // Removed the T_IN_BODYPART state (always use T_PREAMBLE)
        advanceToBoundary();
        if (currentMimePartStream.eof() && !currentMimePartStream.isLastPart()) {
          monitor(Event.MIME_BODY_PREMATURE_END);
        } else {
          if (!currentMimePartStream.isLastPart()) {
            clearMimePartStream();
            createMimePartStream();
            return nextMimeEntity();
          }
        }
        clearMimePartStream();
        state = EntityStates.T_EPILOGUE;
        break;
      case EntityStates.T_EPILOGUE:
        state = EntityStates.T_END_MULTIPART;
        break;
      case EntityStates.T_BODY:
      case EntityStates.T_END_MULTIPART:
        state = endState;
        break;
      default:
        if (state == endState) {
          state = EntityStates.T_END_OF_STREAM;
          break;
        }
        throw new IllegalStateException("Invalid state: " + stateToString(state));
    }
    return null;
  }