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 and Description |
---|
DynamicContentStream(int lines)
Constructor for the example, which takes the
number of lines to generate in the dummy document.
|
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".
|
public DynamicContentStream(int lines)
lines
- Number of lines to put in the
programmatically generated document.public int read() throws java.io.IOException
read
in
class java.io.InputStream
java.io.IOException
- If there is a problem.public int read(byte[] userBuffer, int off, int len) throws java.io.IOException
read
in
class java.io.InputStream
userBuffer
- The buffer to place data in.off
- Index into userBuffer where data should be
placed.len
- The maximum number of bytes to
transfer.java.io.IOException
- Will not happen in this
sample implementation but could conceivably be thrown by
fillBUffer().public int fillBuffer(byte[] buffer)
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.buffer
- The byte array to which data should be
copied.public static void main(java.lang.String[] args) throws java.lang.Exception
args
- Server URI:
xcc://user:password@host:port/contentbase, Doc URI: any valid URI
string to assign to the new content.java.lang.Exception
- If anything bad
happens.
Copyright © 2024 MarkLogic
Corporation
Complete online documentation for MarkLogic Server,
XQuery and related components may
be found at
developer.marklogic.com