AttachmentPart getSize() Method



Description

The Javax.xml.soap.AttachmentPart.getSize() method returns the number of bytes in this AttachmentPart object.

Declaration

Following is the declaration for javax.xml.soap.AttachmentPart.getSize() method

public abstract int getSize()

Parameters

NA

Return Value

This method returns the size of this AttachmentPart object in bytes or -1 if the size cannot be determined

Exception

SOAPException − if the content of this attachment is corrupted of if there was an exception while trying to determine the size

Example

The following example shows the usage of javax.xml.soap.AttachmentPart.getSize() method.

package com.tutorialspoint;

import com.sun.xml.internal.messaging.saaj.util.Base64;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.xml.soap.*;

public class AttachmentPartDemo {

   public static void main(String[] args) {
      try {

         // create a new SOAPMessage
         SOAPMessage message = MessageFactory.newInstance().createMessage();

         // create a string as a new attachment 
         String attachment = "This is an attachment";

         // get the data handler and assign the attachment
         DataHandler handler = new DataHandler(attachment, "HandlerHeader");

         // create the attachment part
         AttachmentPart attachPart = message.createAttachmentPart(handler);

         // get the size of the attachment part
         System.out.println("" + attachPart.getSize());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

If we compile the code and execute it, this will produce the following result −

1024

javax_xml_soap_attachmentpart.htm
Advertisements