com.marklogic.xcc.examples

## Class DynamicContentStream

- java.lang.Object
- - java.io.InputStream
  - - com.marklogic.xcc.examples.DynamicContentStream

- All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

      public class DynamicContentStream
      extends java.io.InputStream

  This class, which extends `InputStream`, is an example of producing dynamic, on-the-fly content that can be used with `ContentFactory.newUnBufferedContent(String, java.io.InputStream, com.marklogic.xcc.ContentCreateOptions)` .

  Click here for the source code for this class

  Like `OutputStreamInserter`, this is an example of inserting dynamically generated or transformed on-the-fly content. The `OutputStreamInserter` example uses `OutputStreamContent` and spawns a thread which then loops and writes to an `OutputStream`.

  By contrast, this example illustrates how to insert dynamic content without creating a new thread. Here, a custom `InputStream` is passed to the standard factory `ContentFactory.newUnBufferedContent(String, java.io.InputStream, com.marklogic.xcc.ContentCreateOptions)` . When `Session.insertContent(com.marklogic.xcc.Content)` is invoked, it will repeatedly call back into this class to obtain chunks of data to send to the server.

  If your data generation can be expressed this way, multiple calls to obtain the data in chunks, it is preferrable to spawning a new thread.

  Note that if your content is potentially large, you should use the above unbuffered factory method. If you call `ContentFactory.newContent(String, java.io.InputStream, com.marklogic.xcc.ContentCreateOptions)` , XCC will first consume and buffer the entire stream before attempting the insert. If the content is unbuffered, then automatic error recovery cannot be performed.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `DynamicContentStream(int lines)` Constructor for the example, which takes the number of lines to generate in the dummy document.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `fillBuffer(byte[] buffer)` This method will be called repeatedly as XCC reads data from the `InputStream` (this object).
    `static void` | `main(java.lang.String[] args)` Example invocation that uses an instance of this class to insert dynamically-generated content, without spawning a separate thread.
    `int` | `read()` Read a single byte.
    `int` | `read(byte[] userBuffer, int off, int len)` Read "len" bytes into "userBuffer", starting at "off".

    - ### Methods inherited from class java.io.InputStream

      `available, close, mark, markSupported, read, reset, skip`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### DynamicContentStream

          public DynamicContentStream(int lines)

      Constructor for the example, which takes the number of lines to generate in the dummy document.
      Parameters:
        `lines` - Number of lines to put in the programmatically generated document.

  - ### Method Detail

    - #### read

          public int read()
                   throws java.io.IOException

      Read a single byte. This method passes through to the byte-array read().
      Specified by:
        `read` in class `java.io.InputStream`
      Returns:
        A byte value, as an integer, or -1 on EOF.
      Throws:
        `java.io.IOException` - If there is a problem.

    - #### read

          public int read(byte[] userBuffer,
                          int off,
                          int len)
                   throws java.io.IOException

      Read "len" bytes into "userBuffer", starting at "off". This method copies bytes from an internal holding buffer out to the provided buffer. On underflow, when the internal buffer is exhausted, the fillBuffer() method is called to obtain more data. Your logic, which may be fetching data from an SQL source or a web service and transforming it on the fly, would placed in fillBUffer().
      Overrides:
        `read` in class `java.io.InputStream`
      Parameters:
        `userBuffer` - The buffer to place data in.
        `off` - Index into userBuffer where data should be placed.
        `len` - The maximum number of bytes to transfer.
      Returns:
        The number of bytes actually transferred, or -1 on EOS.
      Throws:
        `java.io.IOException` - Will not happen in this sample implementation but could conceivably be thrown by fillBUffer().

    - #### fillBuffer

          public int fillBuffer(byte[] buffer)

      This method will be called repeatedly as XCC reads data from the `InputStream` (this object). In a real app, this method would do something more useful, such as reading data from some other source and transforming it. This dummy implementation uses a simple state machine to produce the beginning of a document, then the variable length body, then the end and finally indicates EOS.
      Parameters:
        `buffer` - The byte array to which data should be copied.
      Returns:
        The number of bytes transferred, or -1 if there are no more bytes available.

    - #### main

          public static void main(java.lang.String[] args)
                           throws java.lang.Exception

      Example invocation that uses an instance of this class to insert dynamically-generated content, without spawning a separate thread.
      Parameters:
        `args` - Server URI: xcc://user:password@host:port/contentbase, Doc URI: any valid URI string to assign to the new content.
      Throws:
        `java.lang.Exception` - If anything bad happens.

Copyright © 2025 MarkLogic Corporation

Complete online documentation for MarkLogic Server, XQuery and related components may be found at docs.progress.com