de.undercouch.bson4jackson
Class BsonGenerator

java.lang.Object
  extended by com.fasterxml.jackson.core.JsonGenerator
      extended by com.fasterxml.jackson.core.base.GeneratorBase
          extended by de.undercouch.bson4jackson.BsonGenerator
All Implemented Interfaces:
com.fasterxml.jackson.core.Versioned, java.io.Closeable, java.io.Flushable

public class BsonGenerator
extends com.fasterxml.jackson.core.base.GeneratorBase

Writes BSON code to the provided output stream


Nested Class Summary
static class BsonGenerator.Feature
          Defines toggable features
 
Field Summary
protected  int _bsonFeatures
          Bit flag composed of bits that indicate which BsonGenerator.Features are enabled.
protected  DynamicOutputBuffer _buffer
          Since a BSON document's header must include the size of the whole document in bytes, we have to buffer the whole document first, before we can write it to the output stream.
protected  de.undercouch.bson4jackson.BsonGenerator.DocumentInfo _currentDocument
          Saves information about documents (the main document and embedded ones)
protected  java.io.OutputStream _out
          The output stream to write to
protected  int _typeMarker
          Saves the position of the type marker for the object currently begin written
protected  boolean nextObjectIsEmbeddedInValue
          Indicates that the next object to be encountered is actually embedded inside a value, and not a complete value itself.
 
Fields inherited from class com.fasterxml.jackson.core.base.GeneratorBase
_cfgNumbersAsStrings, _closed, _features, _objectCodec, _writeContext
 
Fields inherited from class com.fasterxml.jackson.core.JsonGenerator
_cfgPrettyPrinter
 
Constructor Summary
BsonGenerator(int jsonFeatures, int bsonFeatures, java.io.OutputStream out)
          Creates a new generator
 
Method Summary
protected  void _releaseBuffers()
           
protected  void _verifyValueWrite(java.lang.String typeMsg)
           
protected  void _writeArrayFieldNameIfNeeded()
          If the generator is currently processing an array, this method writes the field name of the current element (which is just the position of the element in the array)
protected  int _writeCString(java.lang.String string)
          Write a BSON cstring structure (a null terminated string)
protected  void _writeStartObject(boolean array)
          Creates a new embedded document or array
protected  int _writeString(java.lang.String string)
          Write a BSON string structure (a null terminated string prependend by the length of the string)
 void close()
           
protected  java.lang.String flagsToRegexOptions(int flags)
          Converts a a Java flags word into a BSON options pattern
 void flush()
           
protected  void flushBuffer()
          Tries to flush the output buffer if streaming is enabled.
protected  int getAndIncCurrentArrayPos()
          Retrieves and then increases the current position in the array currently being generated
protected  boolean isArray()
           
protected  boolean isEnabled(BsonGenerator.Feature f)
          Checks if a generator feature is enabled
protected  void putHeader(int pos)
          Writes the BSON document header to the output buffer at the given position.
protected  void reserveHeader()
          Reserves bytes for the BSON document header
 void writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len)
           
 void writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, byte subType, byte[] data, int offset, int len)
          Similar to writeBinary(Base64Variant, byte, byte[], int, int), but with the possibility to specify a binary subtype (see BsonConstants).
 void writeBoolean(boolean state)
           
 void writeDateTime(java.util.Date date)
          Write a BSON date time
 void writeEndArray()
           
 void writeEndObject()
           
 void writeFieldName(java.lang.String name)
           
 void writeJavaScript(JavaScript javaScript, com.fasterxml.jackson.databind.SerializerProvider provider)
          Write a BSON JavaScript object
 void writeNull()
           
 void writeNumber(java.math.BigDecimal dec)
           
 void writeNumber(java.math.BigInteger v)
           
 void writeNumber(double d)
           
 void writeNumber(float f)
           
 void writeNumber(int v)
           
 void writeNumber(long v)
           
 void writeNumber(java.lang.String encodedValue)
           
 void writeObjectId(ObjectId objectId)
          Write a BSON ObjectId
 void writeRaw(char c)
           
 void writeRaw(char[] text, int offset, int len)
           
 void writeRaw(java.lang.String text)
           
 void writeRaw(java.lang.String text, int offset, int len)
           
 void writeRawUTF8String(byte[] text, int offset, int length)
           
 void writeRegex(java.util.regex.Pattern pattern)
          Write a BSON regex
 void writeStartArray()
           
 void writeStartObject()
           
 void writeString(char[] text, int offset, int len)
           
 void writeString(java.lang.String text)
           
 void writeSymbol(Symbol symbol)
          Write a BSON Symbol object
 void writeTimestamp(Timestamp timestamp)
          Write a MongoDB timestamp
 void writeUTF8String(byte[] text, int offset, int length)
           
 
Methods inherited from class com.fasterxml.jackson.core.base.GeneratorBase
_reportError, _reportUnsupportedOperation, _throwInternal, _writeSimpleObject, copyCurrentEvent, copyCurrentStructure, disable, enable, getCodec, getOutputContext, isClosed, isEnabled, setCodec, useDefaultPrettyPrinter, version, writeBinary, writeFieldName, writeObject, writeRawValue, writeRawValue, writeRawValue, writeString, writeTree
 
Methods inherited from class com.fasterxml.jackson.core.JsonGenerator
canUseSchema, configure, getCharacterEscapes, getHighestEscapedChar, getOutputTarget, getPrettyPrinter, getSchema, setCharacterEscapes, setHighestNonEscapedChar, setPrettyPrinter, setRootValueSeparator, setSchema, writeArrayFieldStart, writeBinary, writeBinary, writeBinary, writeBinaryField, writeBooleanField, writeNullField, writeNumber, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeObjectField, writeObjectFieldStart, writeRaw, writeStringField
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_bsonFeatures

protected final int _bsonFeatures
Bit flag composed of bits that indicate which BsonGenerator.Features are enabled.


_out

protected final java.io.OutputStream _out
The output stream to write to


_buffer

protected final DynamicOutputBuffer _buffer
Since a BSON document's header must include the size of the whole document in bytes, we have to buffer the whole document first, before we can write it to the output stream. BSON specifies LITTLE_ENDIAN for all tokens.


_typeMarker

protected int _typeMarker
Saves the position of the type marker for the object currently begin written


_currentDocument

protected de.undercouch.bson4jackson.BsonGenerator.DocumentInfo _currentDocument
Saves information about documents (the main document and embedded ones)


nextObjectIsEmbeddedInValue

protected boolean nextObjectIsEmbeddedInValue
Indicates that the next object to be encountered is actually embedded inside a value, and not a complete value itself. This causes things like context validation and writing out the type to be skipped.

Constructor Detail

BsonGenerator

public BsonGenerator(int jsonFeatures,
                     int bsonFeatures,
                     java.io.OutputStream out)
Creates a new generator

Parameters:
jsonFeatures - bit flag composed of bits that indicate which JsonGenerator.Features are enabled.
bsonFeatures - bit flag composed of bits that indicate which BsonGenerator.Features are enabled.
out - the output stream to write to
Method Detail

isEnabled

protected boolean isEnabled(BsonGenerator.Feature f)
Checks if a generator feature is enabled

Parameters:
f - the feature
Returns:
true if the given feature is enabled

isArray

protected boolean isArray()
Returns:
true if the generator is currently processing an array

getAndIncCurrentArrayPos

protected int getAndIncCurrentArrayPos()
Retrieves and then increases the current position in the array currently being generated

Returns:
the position (before it has been increased) or -1 if the current document is not an array

reserveHeader

protected void reserveHeader()
Reserves bytes for the BSON document header


putHeader

protected void putHeader(int pos)
Writes the BSON document header to the output buffer at the given position. Does not increase the buffer's write position.

Parameters:
pos - the position where to write the header

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Specified by:
flush in class com.fasterxml.jackson.core.base.GeneratorBase
Throws:
java.io.IOException

_releaseBuffers

protected void _releaseBuffers()
Specified by:
_releaseBuffers in class com.fasterxml.jackson.core.base.GeneratorBase

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class com.fasterxml.jackson.core.base.GeneratorBase
Throws:
java.io.IOException

writeStartArray

public void writeStartArray()
                     throws java.io.IOException,
                            com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeStartArray in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeEndArray

public void writeEndArray()
                   throws java.io.IOException,
                          com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeEndArray in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeStartObject

public void writeStartObject()
                      throws java.io.IOException,
                             com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeStartObject in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

_writeStartObject

protected void _writeStartObject(boolean array)
                          throws java.io.IOException
Creates a new embedded document or array

Parameters:
array - true if the embedded object is an array
Throws:
java.io.IOException - if the document could not be created

writeEndObject

public void writeEndObject()
                    throws java.io.IOException,
                           com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeEndObject in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

_writeArrayFieldNameIfNeeded

protected void _writeArrayFieldNameIfNeeded()
                                     throws java.io.IOException
If the generator is currently processing an array, this method writes the field name of the current element (which is just the position of the element in the array)

Throws:
java.io.IOException - if the field name could not be written

writeFieldName

public void writeFieldName(java.lang.String name)
                    throws java.io.IOException,
                           com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeFieldName in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

_verifyValueWrite

protected void _verifyValueWrite(java.lang.String typeMsg)
                          throws java.io.IOException
Specified by:
_verifyValueWrite in class com.fasterxml.jackson.core.base.GeneratorBase
Throws:
java.io.IOException

flushBuffer

protected void flushBuffer()
                    throws java.io.IOException
Tries to flush the output buffer if streaming is enabled. This method is a no-op if streaming is disabled.

Throws:
java.io.IOException - if flushing failed

writeString

public void writeString(java.lang.String text)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeString in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeString

public void writeString(char[] text,
                        int offset,
                        int len)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeString in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeRaw

public void writeRaw(java.lang.String text)
              throws java.io.IOException,
                     com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeRaw in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeRaw

public void writeRaw(java.lang.String text,
                     int offset,
                     int len)
              throws java.io.IOException,
                     com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeRaw in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeRaw

public void writeRaw(char[] text,
                     int offset,
                     int len)
              throws java.io.IOException,
                     com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeRaw in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeRaw

public void writeRaw(char c)
              throws java.io.IOException,
                     com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeRaw in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeBinary

public void writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant,
                        byte[] data,
                        int offset,
                        int len)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeBinary in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeBinary

public void writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant,
                        byte subType,
                        byte[] data,
                        int offset,
                        int len)
                 throws java.io.IOException
Similar to writeBinary(Base64Variant, byte, byte[], int, int), but with the possibility to specify a binary subtype (see BsonConstants).

Parameters:
b64variant - base64 variant to use (will be ignored for BSON)
subType - the binary subtype
data - the binary data to write
offset - the offset of the first byte to write
len - the number of bytes to write
Throws:
java.io.IOException - if the binary data could not be written

writeNumber

public void writeNumber(int v)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(long v)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(java.math.BigInteger v)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(double d)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(float f)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(java.math.BigDecimal dec)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNumber

public void writeNumber(java.lang.String encodedValue)
                 throws java.io.IOException,
                        com.fasterxml.jackson.core.JsonGenerationException,
                        java.lang.UnsupportedOperationException
Specified by:
writeNumber in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException
java.lang.UnsupportedOperationException

writeBoolean

public void writeBoolean(boolean state)
                  throws java.io.IOException,
                         com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeBoolean in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeNull

public void writeNull()
               throws java.io.IOException,
                      com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeNull in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeRawUTF8String

public void writeRawUTF8String(byte[] text,
                               int offset,
                               int length)
                        throws java.io.IOException,
                               com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeRawUTF8String in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeUTF8String

public void writeUTF8String(byte[] text,
                            int offset,
                            int length)
                     throws java.io.IOException,
                            com.fasterxml.jackson.core.JsonGenerationException
Specified by:
writeUTF8String in class com.fasterxml.jackson.core.JsonGenerator
Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException

writeDateTime

public void writeDateTime(java.util.Date date)
                   throws java.io.IOException
Write a BSON date time

Parameters:
date - The date to write
Throws:
java.io.IOException - If an error occurred in the stream while writing

writeObjectId

public void writeObjectId(ObjectId objectId)
                   throws java.io.IOException
Write a BSON ObjectId

Parameters:
objectId - The objectId to write
Throws:
java.io.IOException - If an error occurred in the stream while writing

flagsToRegexOptions

protected java.lang.String flagsToRegexOptions(int flags)
Converts a a Java flags word into a BSON options pattern

Parameters:
flags - the Java flags
Returns:
the regex options string

writeRegex

public void writeRegex(java.util.regex.Pattern pattern)
                throws java.io.IOException
Write a BSON regex

Parameters:
pattern - The regex to write
Throws:
java.io.IOException - If an error occurred in the stream while writing

writeTimestamp

public void writeTimestamp(Timestamp timestamp)
                    throws java.io.IOException
Write a MongoDB timestamp

Parameters:
timestamp - The timestamp to write
Throws:
java.io.IOException - If an error occurred in the stream while writing

writeJavaScript

public void writeJavaScript(JavaScript javaScript,
                            com.fasterxml.jackson.databind.SerializerProvider provider)
                     throws java.io.IOException
Write a BSON JavaScript object

Parameters:
javaScript - The javaScript to write
provider - The serializer provider, for serializing the scope
Throws:
java.io.IOException - If an error occurred in the stream while writing

writeSymbol

public void writeSymbol(Symbol symbol)
                 throws java.io.IOException
Write a BSON Symbol object

Parameters:
symbol - The symbol to write
Throws:
java.io.IOException - If an error occurred in the stream while writing

_writeString

protected int _writeString(java.lang.String string)
Write a BSON string structure (a null terminated string prependend by the length of the string)

Parameters:
string - The string to write
Returns:
The number of bytes written, including the terminating null byte and the size of the string

_writeCString

protected int _writeCString(java.lang.String string)
Write a BSON cstring structure (a null terminated string)

Parameters:
string - The string to write
Returns:
The number of bytes written, including the terminating null byte