Class MaapiOutputStream

Object
OutputStream
com.tailf.maapi.MaapiOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class MaapiOutputStream extends OutputStream
Configuration data output stream used to upload configurations. This class is returned as result from Maapi.loadConfigStream(int, java.util.EnumSet).
The application is expected to close the underlying stream socket after usage to assure that background socket is closed use getLocalSocket() to retrieve the socket stream reference.
 // String host = "localhost";
 // int port = Conf.PORT for ConfD or Conf.NCS_PORT for NCS
 Socket s = new Socket(host, port);
 Maapi maapi = new Maapi(s);
 maapi.startUserSession("admin",
     InetAddress.getByName("localhost"),
     "maapi",
     new String[] { "admin" },
     MaapiUserSessionFlag.PROTO_TCP);
 int tid = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ_WRITE);

 MaapiOutputStream outstream =
  maapi.loadConfigStream(tid, EnumSet.of(MaapiConfigFlag.XML_FORMAT));

 FileInputStream fis = new FileInputStream(new File("/tmp/config.xml"));


  byte[] buf = new byte[128];
  int n = -1;

    while((n = fis.read(buf)) != -1){
         outstream.write(buf,0,n);
     }

 fis.close();
 outstream.getLocalSocket().close();

 if(outstream.hasWriteAll())
    System.out.println("All data was uploaded successful!");

 maapi.finishTrans(tid);
 s.close();
 
  • Method Details

    • getLocalSocket

      public Socket getLocalSocket()
      This method is intended to retrieve reference to the underlying stream socket on which the write is performed. To call hasWriteAll() the underlying socket must be closed.
      Returns:
      The Stream socket that this MaapiOutputStream uses.
    • write

      public void write(int b) throws IOException
      Write a byte from the input stream or -1 if EOF
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Write a portion of an array of bytes.
      Overrides:
      write in class OutputStream
      Parameters:
      b - Buffer of byte
      off - Offset from which to start writing bytes
      len - Number of characters to write
      Throws:
      IOException
    • hasWriteAll

      public boolean hasWriteAll()
      Checks with the server if the complete configuration is uploaded. This is not performed by reading the stream to EOF but instead a deliberate call to the server. NOTE: Prior to calling this method it is important that the stream socket is closed. Use the getLocalSocket() to retrieve the stream socket or the method will hang until the socket is closed. If this call returns false, call getErrorString() and getErrorCode() to get the error message and the error code respectivly.
      Returns:
      boolean true if complete configuration is downloaded
    • getErrorString

      public String getErrorString()
      This method can be called after a call to hasWriteAll() to get the error message in case hasWriteAll() returns false, which is the case when writing to the output stream fails. NOTE: This function will only return useful information after hasWriteAll() has been called and returned false.
      Returns:
      the error string in case of an error when writing to the output stream
    • getErrorCode

      public ErrorCode getErrorCode()
      This method can be called after a call to hasWriteAll() to get the error code in case hasWriteAll() returns false, which is the case when writing to the output stream fails. NOTE: This function will only return useful information after hasWriteAll() has been called and returned false.
      Returns:
      the error code in case of an error when writing to the output stream