Overview | Package | Class | Tree | Deprecated | Index | Help
| XCC/J 4.2-9
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.marklogic.xcc.examples
Class DynamicContentStream

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

All Implemented Interfaces:
  Closeable

    public class DynamicContentStream

extends 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
`DynamicContentStream(int lines)` Constructor for the example, which takes the number of lines to generate in the dummy document.

Method Summary
`int` | `fillBuffer(byte[] buffer)` This method will be called repeatedly as XCC reads data from the `InputStream` (this object).
`static void` | `main(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 IOException

  Read a single byte. This method passes through to the byte-array read().

  Specified by:
    `read` in class `InputStream`
  Returns:
    A byte value, as an integer, or -1 on EOF.
  Throws:
    `IOException` - If there is a problem.

### read

    public int read(byte[] userBuffer,
                    int off,
                    int len)
             throws 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 `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:
    `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(String[] args)
                     throws 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:
    `Exception` - If anything bad happens.

Overview | Package | Class | Tree | Deprecated | Index | Help
| XCC/J 4.2-9
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2012 MarkLogic Corporation, All Rights Reserved.

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