Package org.bson
Class AbstractBsonWriter
java.lang.Object
org.bson.AbstractBsonWriter
- All Implemented Interfaces:
Closeable,AutoCloseable,BsonWriter
- Direct Known Subclasses:
BsonBinaryWriter,BsonDocumentWriter,JsonWriter
Represents a BSON writer for some external format (see subclasses).
- Since:
- 3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThe context for the writer.protected classCapture the current state of this writer - itsAbstractBsonWriter.Context,AbstractBsonWriter.State, field name and depth.static enumThe state of a writer. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractBsonWriter(BsonWriterSettings settings) Initializes a new instance of the BsonWriter class.protectedAbstractBsonWriter(BsonWriterSettings settings, FieldNameValidator validator) Initializes a new instance of the BsonWriter class. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanReturn true if the current execution of the pipe method should be aborted.protected voidcheckPreconditions(String methodName, AbstractBsonWriter.State... validStates) Checks the writer is in the correct state.protected booleancheckState(AbstractBsonWriter.State[] validStates) Checks if this writer's current state is in the list of given states.voidclose()protected abstract voiddoWriteBinaryData(BsonBinary value) Handles the logic of writing aBsonBinaryvalueprotected abstract voiddoWriteBoolean(boolean value) Handles the logic of writing a boolean valueprotected abstract voiddoWriteDateTime(long value) Handles the logic of writing a date time valueprotected abstract voiddoWriteDBPointer(BsonDbPointer value) Handles the logic of writing a DbPointer valueprotected abstract voiddoWriteDecimal128(Decimal128 value) Handles the logic of writing a Decimal128 valueprotected abstract voiddoWriteDouble(double value) Handles the logic of writing a Double valueprotected abstract voidHandles the logic of writing the end of an arrayprotected abstract voidHandles the logic of writing the end of a documentprotected abstract voiddoWriteInt32(int value) Handles the logic of writing an int32 valueprotected abstract voiddoWriteInt64(long value) Handles the logic of writing an int64 valueprotected abstract voiddoWriteJavaScript(String value) Handles the logic of writing a JavaScript functionprotected abstract voiddoWriteJavaScriptWithScope(String value) Handles the logic of writing a scoped JavaScript functionprotected abstract voidHandles the logic of writing a Max keyprotected abstract voidHandles the logic of writing a Min keyprotected voiddoWriteName(String name) Handles the logic of writing the element name.protected abstract voidHandles the logic of writing a Null valueprotected abstract voiddoWriteObjectId(ObjectId value) Handles the logic of writing an ObjectIdprotected abstract voidHandles the logic of writing a regular expressionprotected abstract voidHandles the logic to start writing an arrayprotected abstract voidHandles the logic to start writing a documentprotected abstract voiddoWriteString(String value) Handles the logic of writing a Stringprotected abstract voiddoWriteSymbol(String value) Handles the logic of writing a Symbolprotected abstract voiddoWriteTimestamp(BsonTimestamp value) Handles the logic of writing a timestampprotected abstract voidHandles the logic of writing an Undefined valuevoidflush()Flushes any pending data to the output destination.protected AbstractBsonWriter.ContextGet the context, which will indicate which state the writer is in, for example which part of a document it's currently writing.protected StringgetName()The name of the field being written.protected AbstractBsonWriter.StateReturns the next valid state for this writer.protected AbstractBsonWriter.StategetState()Gets the current state of this writer.protected booleanisClosed()Returns whether this writer has been closed.voidpipe(BsonReader reader) Reads a single document from a BsonReader and writes it to this.voidpipe(BsonReader reader, List<BsonElement> extraElements) Reads a single document from the given BsonReader and writes it to this, appending the given extra elements to the document.protected voidpipeExtraElements(List<BsonElement> extraElements) Pipe a list of extra element to this writerprotected voidsetContext(AbstractBsonWriter.Context context) Set the context, which will indicate which state the writer is in, for example which part of a document it's currently writing.protected voidsetState(AbstractBsonWriter.State state) Sets the current state of the writer.protected voidthrowInvalidContextType(String methodName, BsonContextType actualContextType, BsonContextType... validContextTypes) Throws an InvalidOperationException when the method called is not valid for the current ContextType.protected voidthrowInvalidState(String methodName, AbstractBsonWriter.State... validStates) Throws aBsonInvalidOperationExceptionwhen the method called is not valid for the current state.voidwriteBinaryData(String name, BsonBinary binary) Writes a BSON Binary data element to the writer.voidwriteBinaryData(BsonBinary binary) Writes a BSON Binary data element to the writer.voidwriteBoolean(boolean value) Writes a BSON Boolean to the writer.voidwriteBoolean(String name, boolean value) Writes a BSON Boolean element to the writer.voidwriteDateTime(long value) Writes a BSON DateTime to the writer.voidwriteDateTime(String name, long value) Writes a BSON DateTime element to the writer.voidwriteDBPointer(String name, BsonDbPointer value) Writes a BSON DBPointer element to the writer.voidwriteDBPointer(BsonDbPointer value) Writes a BSON DBPointer to the writer.voidwriteDecimal128(String name, Decimal128 value) Writes a BSON Decimal128 element to the writer.voidwriteDecimal128(Decimal128 value) Writes a BSON Decimal128 to the writer.voidwriteDouble(double value) Writes a BSON Double to the writer.voidwriteDouble(String name, double value) Writes a BSON Double element to the writer.voidWrites the end of a BSON array to the writer.voidWrites the end of a BSON document to the writer.voidwriteInt32(int value) Writes a BSON Int32 to the writer.voidwriteInt32(String name, int value) Writes a BSON Int32 element to the writer.voidwriteInt64(long value) Writes a BSON Int64 to the writer.voidwriteInt64(String name, long value) Writes a BSON Int64 element to the writer.voidwriteJavaScript(String code) Writes a BSON JavaScript to the writer.voidwriteJavaScript(String name, String code) Writes a BSON JavaScript element to the writer.voidWrites a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope).voidwriteJavaScriptWithScope(String name, String code) Writes a BSON JavaScript element to the writer (call WriteStartDocument to start writing the scope).voidWrites a BSON MaxKey to the writer.voidwriteMaxKey(String name) Writes a BSON MaxKey element to the writer.voidWrites a BSON MinKey to the writer.voidwriteMinKey(String name) Writes a BSON MinKey element to the writer.voidWrites the name of an element to the writer.voidWrites a BSON null to the writer.voidWrites a BSON null element to the writer.voidwriteObjectId(String name, ObjectId objectId) Writes a BSON ObjectId element to the writer.voidwriteObjectId(ObjectId objectId) Writes a BSON ObjectId to the writer.voidwriteRegularExpression(String name, BsonRegularExpression regularExpression) Writes a BSON regular expression element to the writer.voidwriteRegularExpression(BsonRegularExpression regularExpression) Writes a BSON regular expression to the writer.voidWrites the start of a BSON array to the writer.voidwriteStartArray(String name) Writes the start of a BSON array element to the writer.voidWrites the start of a BSON document to the writer.voidwriteStartDocument(String name) Writes the start of a BSON document element to the writer.voidwriteString(String value) Writes a BSON String to the writer.voidwriteString(String name, String value) Writes a BSON String element to the writer.voidwriteSymbol(String value) Writes a BSON Symbol to the writer.voidwriteSymbol(String name, String value) Writes a BSON Symbol element to the writer.voidwriteTimestamp(String name, BsonTimestamp value) Writes a BSON Timestamp element to the writer.voidwriteTimestamp(BsonTimestamp value) Writes a BSON Timestamp to the writer.voidWrites a BSON undefined to the writer.voidwriteUndefined(String name) Writes a BSON undefined element to the writer.
-
Constructor Details
-
AbstractBsonWriter
Initializes a new instance of the BsonWriter class.- Parameters:
settings- The writer settings.
-
AbstractBsonWriter
Initializes a new instance of the BsonWriter class.- Parameters:
settings- The writer settings.validator- the field name validator
-
-
Method Details
-
getName
The name of the field being written.- Returns:
- the name of the field
-
isClosed
protected boolean isClosed()Returns whether this writer has been closed.- Returns:
- true if the
close()method has been called.
-
setState
Sets the current state of the writer. The current state determines what sort of actions are valid for this writer at this time.- Parameters:
state- the state to set this writer to.
-
getState
Gets the current state of this writer. The current state determines what sort of actions are valid for this writer at this time.- Returns:
- the current state of the writer.
-
getContext
Get the context, which will indicate which state the writer is in, for example which part of a document it's currently writing.- Returns:
- the current context.
-
setContext
Set the context, which will indicate which state the writer is in, for example which part of a document it's currently writing.- Parameters:
context- the new context for this writer
-
doWriteStartDocument
protected abstract void doWriteStartDocument()Handles the logic to start writing a document -
doWriteEndDocument
protected abstract void doWriteEndDocument()Handles the logic of writing the end of a document -
doWriteStartArray
protected abstract void doWriteStartArray()Handles the logic to start writing an array -
doWriteEndArray
protected abstract void doWriteEndArray()Handles the logic of writing the end of an array -
doWriteBinaryData
Handles the logic of writing aBsonBinaryvalue- Parameters:
value- theBsonBinaryvalue to write
-
doWriteBoolean
protected abstract void doWriteBoolean(boolean value) Handles the logic of writing a boolean value- Parameters:
value- thebooleanvalue to write
-
doWriteDateTime
protected abstract void doWriteDateTime(long value) Handles the logic of writing a date time value- Parameters:
value- thelongvalue to write
-
doWriteDBPointer
Handles the logic of writing a DbPointer value- Parameters:
value- theBsonDbPointervalue to write
-
doWriteDouble
protected abstract void doWriteDouble(double value) Handles the logic of writing a Double value- Parameters:
value- thedoublevalue to write
-
doWriteInt32
protected abstract void doWriteInt32(int value) Handles the logic of writing an int32 value- Parameters:
value- theintvalue to write
-
doWriteInt64
protected abstract void doWriteInt64(long value) Handles the logic of writing an int64 value- Parameters:
value- thelongvalue to write
-
doWriteDecimal128
Handles the logic of writing a Decimal128 value- Parameters:
value- theDecimal128value to write- Since:
- 3.4
-
doWriteJavaScript
Handles the logic of writing a JavaScript function- Parameters:
value- theStringvalue to write
-
doWriteJavaScriptWithScope
Handles the logic of writing a scoped JavaScript function- Parameters:
value- thebooleanvalue to write
-
doWriteMaxKey
protected abstract void doWriteMaxKey()Handles the logic of writing a Max key -
doWriteMinKey
protected abstract void doWriteMinKey()Handles the logic of writing a Min key -
doWriteNull
protected abstract void doWriteNull()Handles the logic of writing a Null value -
doWriteObjectId
Handles the logic of writing an ObjectId- Parameters:
value- theObjectIdvalue to write
-
doWriteRegularExpression
Handles the logic of writing a regular expression- Parameters:
value- theBsonRegularExpressionvalue to write
-
doWriteString
Handles the logic of writing a String- Parameters:
value- theStringvalue to write
-
doWriteSymbol
Handles the logic of writing a Symbol- Parameters:
value- thebooleanvalue to write
-
doWriteTimestamp
Handles the logic of writing a timestamp- Parameters:
value- theBsonTimestampvalue to write
-
doWriteUndefined
protected abstract void doWriteUndefined()Handles the logic of writing an Undefined value -
writeStartDocument
Description copied from interface:BsonWriterWrites the start of a BSON document element to the writer.- Specified by:
writeStartDocumentin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeStartDocument
public void writeStartDocument()Description copied from interface:BsonWriterWrites the start of a BSON document to the writer.- Specified by:
writeStartDocumentin interfaceBsonWriter
-
writeEndDocument
public void writeEndDocument()Description copied from interface:BsonWriterWrites the end of a BSON document to the writer.- Specified by:
writeEndDocumentin interfaceBsonWriter
-
writeStartArray
Description copied from interface:BsonWriterWrites the start of a BSON array element to the writer.- Specified by:
writeStartArrayin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeStartArray
public void writeStartArray()Description copied from interface:BsonWriterWrites the start of a BSON array to the writer.- Specified by:
writeStartArrayin interfaceBsonWriter
-
writeEndArray
public void writeEndArray()Description copied from interface:BsonWriterWrites the end of a BSON array to the writer.- Specified by:
writeEndArrayin interfaceBsonWriter
-
writeBinaryData
Description copied from interface:BsonWriterWrites a BSON Binary data element to the writer.- Specified by:
writeBinaryDatain interfaceBsonWriter- Parameters:
name- The name of the element.binary- The Binary data value.
-
writeBinaryData
Description copied from interface:BsonWriterWrites a BSON Binary data element to the writer.- Specified by:
writeBinaryDatain interfaceBsonWriter- Parameters:
binary- The Binary data.
-
writeBoolean
Description copied from interface:BsonWriterWrites a BSON Boolean element to the writer.- Specified by:
writeBooleanin interfaceBsonWriter- Parameters:
name- The name of the element.value- The Boolean value.
-
writeBoolean
public void writeBoolean(boolean value) Description copied from interface:BsonWriterWrites a BSON Boolean to the writer.- Specified by:
writeBooleanin interfaceBsonWriter- Parameters:
value- The Boolean value.
-
writeDateTime
Description copied from interface:BsonWriterWrites a BSON DateTime element to the writer.- Specified by:
writeDateTimein interfaceBsonWriter- Parameters:
name- The name of the element.value- The number of milliseconds since the Unix epoch.
-
writeDateTime
public void writeDateTime(long value) Description copied from interface:BsonWriterWrites a BSON DateTime to the writer.- Specified by:
writeDateTimein interfaceBsonWriter- Parameters:
value- The number of milliseconds since the Unix epoch.
-
writeDBPointer
Description copied from interface:BsonWriterWrites a BSON DBPointer element to the writer.- Specified by:
writeDBPointerin interfaceBsonWriter- Parameters:
name- The name of the element.value- The DBPointer to write
-
writeDBPointer
Description copied from interface:BsonWriterWrites a BSON DBPointer to the writer.- Specified by:
writeDBPointerin interfaceBsonWriter- Parameters:
value- The DBPointer to write
-
writeDouble
Description copied from interface:BsonWriterWrites a BSON Double element to the writer.- Specified by:
writeDoublein interfaceBsonWriter- Parameters:
name- The name of the element.value- The Double value.
-
writeDouble
public void writeDouble(double value) Description copied from interface:BsonWriterWrites a BSON Double to the writer.- Specified by:
writeDoublein interfaceBsonWriter- Parameters:
value- The Double value.
-
writeInt32
Description copied from interface:BsonWriterWrites a BSON Int32 element to the writer.- Specified by:
writeInt32in interfaceBsonWriter- Parameters:
name- The name of the element.value- The Int32 value.
-
writeInt32
public void writeInt32(int value) Description copied from interface:BsonWriterWrites a BSON Int32 to the writer.- Specified by:
writeInt32in interfaceBsonWriter- Parameters:
value- The Int32 value.
-
writeInt64
Description copied from interface:BsonWriterWrites a BSON Int64 element to the writer.- Specified by:
writeInt64in interfaceBsonWriter- Parameters:
name- The name of the element.value- The Int64 value.
-
writeInt64
public void writeInt64(long value) Description copied from interface:BsonWriterWrites a BSON Int64 to the writer.- Specified by:
writeInt64in interfaceBsonWriter- Parameters:
value- The Int64 value.
-
writeDecimal128
Description copied from interface:BsonWriterWrites a BSON Decimal128 to the writer.- Specified by:
writeDecimal128in interfaceBsonWriter- Parameters:
value- The Decimal128 value.
-
writeDecimal128
Description copied from interface:BsonWriterWrites a BSON Decimal128 element to the writer.- Specified by:
writeDecimal128in interfaceBsonWriter- Parameters:
name- The name of the element.value- The Decimal128 value.
-
writeJavaScript
Description copied from interface:BsonWriterWrites a BSON JavaScript element to the writer.- Specified by:
writeJavaScriptin interfaceBsonWriter- Parameters:
name- The name of the element.code- The JavaScript code.
-
writeJavaScript
Description copied from interface:BsonWriterWrites a BSON JavaScript to the writer.- Specified by:
writeJavaScriptin interfaceBsonWriter- Parameters:
code- The JavaScript code.
-
writeJavaScriptWithScope
Description copied from interface:BsonWriterWrites a BSON JavaScript element to the writer (call WriteStartDocument to start writing the scope).- Specified by:
writeJavaScriptWithScopein interfaceBsonWriter- Parameters:
name- The name of the element.code- The JavaScript code.
-
writeJavaScriptWithScope
Description copied from interface:BsonWriterWrites a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope).- Specified by:
writeJavaScriptWithScopein interfaceBsonWriter- Parameters:
code- The JavaScript code.
-
writeMaxKey
Description copied from interface:BsonWriterWrites a BSON MaxKey element to the writer.- Specified by:
writeMaxKeyin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeMaxKey
public void writeMaxKey()Description copied from interface:BsonWriterWrites a BSON MaxKey to the writer.- Specified by:
writeMaxKeyin interfaceBsonWriter
-
writeMinKey
Description copied from interface:BsonWriterWrites a BSON MinKey element to the writer.- Specified by:
writeMinKeyin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeMinKey
public void writeMinKey()Description copied from interface:BsonWriterWrites a BSON MinKey to the writer.- Specified by:
writeMinKeyin interfaceBsonWriter
-
writeName
Description copied from interface:BsonWriterWrites the name of an element to the writer.- Specified by:
writeNamein interfaceBsonWriter- Parameters:
name- The name of the element.
-
doWriteName
Handles the logic of writing the element name.- Parameters:
name- the name of the element- Since:
- 3.5
-
writeNull
Description copied from interface:BsonWriterWrites a BSON null element to the writer.- Specified by:
writeNullin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeNull
public void writeNull()Description copied from interface:BsonWriterWrites a BSON null to the writer.- Specified by:
writeNullin interfaceBsonWriter
-
writeObjectId
Description copied from interface:BsonWriterWrites a BSON ObjectId element to the writer.- Specified by:
writeObjectIdin interfaceBsonWriter- Parameters:
name- The name of the element.objectId- The ObjectId value.
-
writeObjectId
Description copied from interface:BsonWriterWrites a BSON ObjectId to the writer.- Specified by:
writeObjectIdin interfaceBsonWriter- Parameters:
objectId- The ObjectId value.
-
writeRegularExpression
Description copied from interface:BsonWriterWrites a BSON regular expression element to the writer.- Specified by:
writeRegularExpressionin interfaceBsonWriter- Parameters:
name- The name of the element.regularExpression- The RegularExpression value.
-
writeRegularExpression
Description copied from interface:BsonWriterWrites a BSON regular expression to the writer.- Specified by:
writeRegularExpressionin interfaceBsonWriter- Parameters:
regularExpression- the regular expression to write.
-
writeString
Description copied from interface:BsonWriterWrites a BSON String element to the writer.- Specified by:
writeStringin interfaceBsonWriter- Parameters:
name- The name of the element.value- The String value.
-
writeString
Description copied from interface:BsonWriterWrites a BSON String to the writer.- Specified by:
writeStringin interfaceBsonWriter- Parameters:
value- The String value.
-
writeSymbol
Description copied from interface:BsonWriterWrites a BSON Symbol element to the writer.- Specified by:
writeSymbolin interfaceBsonWriter- Parameters:
name- The name of the element.value- The symbol.
-
writeSymbol
Description copied from interface:BsonWriterWrites a BSON Symbol to the writer.- Specified by:
writeSymbolin interfaceBsonWriter- Parameters:
value- The symbol.
-
writeTimestamp
Description copied from interface:BsonWriterWrites a BSON Timestamp element to the writer.- Specified by:
writeTimestampin interfaceBsonWriter- Parameters:
name- The name of the element.value- The combined timestamp/increment value.
-
writeTimestamp
Description copied from interface:BsonWriterWrites a BSON Timestamp to the writer.- Specified by:
writeTimestampin interfaceBsonWriter- Parameters:
value- The combined timestamp/increment value.
-
writeUndefined
Description copied from interface:BsonWriterWrites a BSON undefined element to the writer.- Specified by:
writeUndefinedin interfaceBsonWriter- Parameters:
name- The name of the element.
-
writeUndefined
public void writeUndefined()Description copied from interface:BsonWriterWrites a BSON undefined to the writer.- Specified by:
writeUndefinedin interfaceBsonWriter
-
getNextState
Returns the next valid state for this writer. For example, transitions fromAbstractBsonWriter.State.VALUEtoAbstractBsonWriter.State.NAMEonce a value is written.- Returns:
- the next
State
-
checkState
Checks if this writer's current state is in the list of given states.- Parameters:
validStates- an array ofStates to compare this writer's state to.- Returns:
- true if this writer's state is in the given list.
-
checkPreconditions
Checks the writer is in the correct state. If the writer's current state is in the list of given states, this method will complete without exception. Throws anIllegalStateExceptionif the writer is closed. Throws BsonInvalidOperationException if the method is trying to do something that is not permitted in the current state.- Parameters:
methodName- the name of the method being performed that checks are being performed forvalidStates- the list of valid states for this operation- See Also:
-
throwInvalidContextType
protected void throwInvalidContextType(String methodName, BsonContextType actualContextType, BsonContextType... validContextTypes) Throws an InvalidOperationException when the method called is not valid for the current ContextType.- Parameters:
methodName- The name of the method.actualContextType- The actual ContextType.validContextTypes- The valid ContextTypes.- Throws:
BsonInvalidOperationException- when the method called is not valid for the current ContextType.
-
throwInvalidState
Throws aBsonInvalidOperationExceptionwhen the method called is not valid for the current state.- Parameters:
methodName- The name of the method.validStates- The valid states.- Throws:
BsonInvalidOperationException- when the method called is not valid for the current state.
-
flush
public void flush()Flushes any pending data to the output destination.The
flush()method ofAbstractBsonWriterdoes nothing.- Specified by:
flushin interfaceBsonWriter
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
pipe
Description copied from interface:BsonWriterReads a single document from a BsonReader and writes it to this.- Specified by:
pipein interfaceBsonWriter- Parameters:
reader- The source.
-
pipe
Reads a single document from the given BsonReader and writes it to this, appending the given extra elements to the document.- Parameters:
reader- the source of the documentextraElements- the extra elements to append to the document- Since:
- 3.6
-
pipeExtraElements
Pipe a list of extra element to this writer- Parameters:
extraElements- the extra elements
-
abortPipe
protected boolean abortPipe()Return true if the current execution of the pipe method should be aborted.- Returns:
- true if the current execution of the pipe method should be aborted.
- Since:
- 3.7
-