FileDocCategorySizeDatePackage
MultiPartDimeInputStream.javaAPI DocApache Axis 1.48510Sat Apr 22 18:57:28 BST 2006org.apache.axis.attachments

MultiPartDimeInputStream

public class MultiPartDimeInputStream extends MultiPartInputStream
This simulates the multipart stream.
author
Rick Rineholt

Fields Summary
protected static Log
log
protected HashMap
parts
protected LinkedList
orderedParts
protected int
rootPartLength
protected boolean
closed
protected boolean
eos
protected DimeDelimitedInputStream
dimeDelimitedStream
protected InputStream
soapStream
protected byte[]
boundary
protected ByteArrayInputStream
cachedSOAPEnvelope
protected String
contentId
protected static final String[]
READ_ALL
Constructors Summary
public MultiPartDimeInputStream(InputStream is)
Create a new Multipart stream from an input stream.

param
is the true input stream that is read from
throws
java.io.IOException if it was not possible to build the Multipart


                                       
       
        
        super(null); //don't cache this stream.
        soapStream = dimeDelimitedStream = new DimeDelimitedInputStream(is); //The Soap stream must always be first
        contentId = dimeDelimitedStream.getContentId();
    
Methods Summary
protected voidaddPart(java.lang.String contentId, java.lang.String locationId, AttachmentPart ap)

     //For DIME streams Content-Location is ignored.
        if (contentId != null && contentId.trim().length() != 0)
          parts.put(contentId, ap);
        orderedParts.add(ap);
    
public voidclose()

        closed = true;
        soapStream.close();
    
public org.apache.axis.PartgetAttachmentByReference(java.lang.String[] id)

        //First see if we have read it in yet.
        Part ret = null;

        try {
            for (int i = id.length - 1; ret == null && i > -1; --i) {
                ret = (AttachmentPart) parts.get(id[i]);
            }

            if (null == ret) {
                ret = readTillFound(id);
            }
            log.debug(Messages.getMessage("return02",
                    "getAttachmentByReference(\"" + id + "\"",
                    (ret == null ? "null" : ret.toString())));
        } catch (java.io.IOException e) {
            throw new org.apache.axis.AxisFault(e.getClass().getName()
              + e.getMessage());
        }
        return ret;
    
public java.util.CollectiongetAttachments()

        readAll();
        return new java.util.LinkedList(orderedParts);
    
public java.lang.StringgetContentId()
Return the content id of the stream.

return
the Content-Location of the stream. Null if no content-location specified.

        return contentId;
    
public java.lang.StringgetContentLocation()
Return the content location.

return
the Content-Location of the stream. Null if no content-location specified.

        return null;
    
public intread(byte[] b)

        return read(b, 0, b.length);
    
public intread()

        if (closed) {
            throw new java.io.IOException(Messages.getMessage(
            "streamClosed"));
        }
        if (eos) {
            return -1;
        }
        int ret = soapStream.read();

        if (ret < 0) {
            eos = true;
        }
        return ret;
    
public intread(byte[] b, int off, int len)

        if (closed) {
            throw new java.io.IOException(Messages.getMessage(
              "streamClosed"));
        }
        if (eos) {
            return -1;
        }
        int read = soapStream.read(b, off, len);

        if (read < 0) {
            eos = true;
        }
        return read;
    
protected voidreadAll()


         
        try {
            readTillFound(READ_ALL);
        } catch (Exception e) {
            throw org.apache.axis.AxisFault.makeFault(e);
        }
    
protected org.apache.axis.PartreadTillFound(java.lang.String[] id)
This will read streams in till the one that is needed is found.

param
id is the stream being sought
return
a Part matching the ids

        if (dimeDelimitedStream == null) {
            //The whole stream has been consumed already
            return null;
        }
        Part ret = null;

        try {

            if (soapStream != null) { //Still on the SOAP stream.
                if (!eos) { //The SOAP packet has not been fully read yet. Need to store it away.

                    java.io.ByteArrayOutputStream soapdata =
                      new java.io.ByteArrayOutputStream(1024 * 8);

                    byte[] buf = new byte[1024 * 16];
                    int byteread = 0;

                    do {
                        byteread = soapStream.read(buf);
                        if (byteread > 0) {
                            soapdata.write(buf, 0, byteread);
                        }

                    } while (byteread > -1);
                    soapdata.close();
                    soapStream.close();
                    soapStream = new java.io.ByteArrayInputStream(
                     soapdata.toByteArray());
                }
                dimeDelimitedStream = dimeDelimitedStream.getNextStream();
            }
            //Now start searching for the data.

            if (null != dimeDelimitedStream) {
                do {
                    String contentId = dimeDelimitedStream.getContentId();
                    String type = dimeDelimitedStream.getType();

                    if (type != null && !dimeDelimitedStream.getDimeTypeNameFormat().equals(DimeTypeNameFormat.MIME)) {
                        type = "application/uri; uri=\"" + type + "\"";
                    }


                    ManagedMemoryDataSource source = new ManagedMemoryDataSource(dimeDelimitedStream,
                                          ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, type, true);
                    DataHandler dh = new DataHandler(source);

                    AttachmentPart ap = new AttachmentPart(dh);
                    if (contentId != null) {
                        ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, contentId);
                    }

                    addPart(contentId, "", ap);

                    for (int i = id.length - 1; ret == null && i > -1; --i) {
                        if (contentId != null && id[i].equals(contentId)) { //This is the part being sought
                            ret = ap;
                        }
                    }

                    dimeDelimitedStream =
                     dimeDelimitedStream.getNextStream();

                }
                while (null == ret && null != dimeDelimitedStream);
            }
        } catch (Exception e) {
            throw org.apache.axis.AxisFault.makeFault(e);
        }

        return ret;