Examples of ByteOutputStream


Examples of com.sun.xml.messaging.saaj.util.ByteOutputStream

        String boundary = "--" + contentType.getParameter("boundary");
  byte[] bndbytes = ASCIIUtility.getBytes(boundary);
  int bl = bndbytes.length;

  ByteOutputStream buf = null;
  try {
      // Skip the preamble
      LineInputStream lin = new LineInputStream(in);
      String line;
      while ((line = lin.readLine()) != null) {
    /*
     * Strip trailing whitespace.  Can't use trim method
     * because it's too aggressive.  Some bogus MIME
     * messages will include control characters in the
     * boundary string.
     */
    int i;
    for (i = line.length() - 1; i >= 0; i--) {
        char c = line.charAt(i);
        if (!(c == ' ' || c == '\t'))
      break;
    }
    line = line.substring(0, i + 1);
    if (line.equals(boundary))
        break;
      }
      if (line == null)
    throw new MessagingException("Missing start boundary");
     
      /*
       * Read and process body parts until we see the
       * terminating boundary line (or EOF).
       */
      boolean done = false;
  getparts:
      while (!done) {
    InternetHeaders headers = null;
    if (sin != null) {
        start = sin.getPosition();
        // skip headers
        while ((line = lin.readLine()) != null && line.length() > 0)
      ;
        if (line == null) {
                        if (!ignoreMissingEndBoundary) {
                           throw new MessagingException("Missing End Boundary for Mime Package : EOF while skipping headers");
                        }
      // assume there's just a missing end boundary
      break getparts;
        }
    } else {
        // collect the headers for this body part
        headers = createInternetHeaders(in);
    }

    if (!in.markSupported())
        throw new MessagingException("Stream doesn't support mark");

    buf = null;
    // if we don't have a shared input stream, we copy the data
    if (sin == null)
        buf = new ByteOutputStream();
    int b;
    boolean bol = true;    // beginning of line flag
    // the two possible end of line characters
    int eol1 = -1, eol2 = -1;

    /*
     * Read and save the content bytes in buf.
     */
    for (;;) {
        if (bol) {
      /*
       * At the beginning of a line, check whether the
       * next line is a boundary.
       */
      int i;
      in.mark(bl + 4 + 1000); // bnd + "--\r\n" + lots of LWSP
      // read bytes, matching against the boundary
      for (i = 0; i < bl; i++)
          if (in.read() != bndbytes[i])
        break;
      if (i == bl) {
          // matched the boundary, check for last boundary
          int b2 = in.read();
          if (b2 == '-') {
        if (in.read() == '-') {
            done = true;
                                    foundClosingBoundary = true;
            break// ignore trailing text
        }
          }
          // skip linear whitespace
          while (b2 == ' ' || b2 == '\t')
        b2 = in.read();
          // check for end of line
          if (b2 == '\n')
        break// got it!  break out of the loop
          if (b2 == '\r') {
        in.mark(1);
        if (in.read() != '\n')
            in.reset();
        break// got it!  break out of the loop
          }
      }
      // failed to match, reset and proceed normally
      in.reset();

      // if this is not the first line, write out the
      // end of line characters from the previous line
      if (buf != null && eol1 != -1) {
          buf.write(eol1);
          if (eol2 != -1)
        buf.write(eol2);
          eol1 = eol2 = -1;
      }
        }

        // read the next byte
        if ((b = in.read()) < 0) {
      done = true;
      break;
        }

        /*
         * If we're at the end of the line, save the eol characters
         * to be written out before the beginning of the next line.
         */
        if (b == '\r' || b == '\n') {
      bol = true;
      if (sin != null)
          end = sin.getPosition() - 1;
      eol1 = b;
      if (b == '\r') {
          in.mark(1);
          if ((b = in.read()) == '\n')
        eol2 = b;
          else
        in.reset();
      }
        } else {
      bol = false;
      if (buf != null)
          buf.write(b);
        }
    }

    /*
     * Create a MimeBody element to represent this body part.
     */
    MimeBodyPart part;
    if (sin != null)
        part = createMimeBodyPart(sin.newStream(start, end));
    else
        part = createMimeBodyPart(headers, buf.getBytes(), buf.getCount());
    addBodyPart(part);
      }
  } catch (IOException ioex) {
      throw new MessagingException("IO Error", ioex);
  } finally {
      if (buf != null)
          buf.close();
  }

        if (!ignoreMissingEndBoundary && !foundClosingBoundary && sin== null) {
            throw new MessagingException("Missing End Boundary for Mime Package : EOF while skipping headers");
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.