Contents
- Description
- Field Summary
- Constructor Summary
- Method Summary
- Field Details
- Constructor Details
- Method Details
- writeToBytes(BsonDocument)
- encodeInto(BsonDocument, BasicOutputBuffer)
- readFromBytes(byte[])
- readFromBuffer(ByteBuffer)
- readFromBinaryStream(ByteBuffer)
- writeToBinaryStream(DataOutputStream, BsonDocument)
- writeDocument(Path, BsonDocument)
- writeDocument(Path, BsonDocument, boolean)
- readDocument(Path)
- readDocument(Path, boolean)
- readDocumentNow(Path)
- readDocumentBak(Path)
- parseWithMaxDepth(String)
- parseWithMaxDepth(String, int)
- translateJsonToBson(JsonElement)
- translateBsonToJson(BsonDocument)
- toJson(BsonDocument)
- writeSync(Path, Codec, T, HytaleLogger)
Class BsonUtil
java.lang.Object
com.hypixel.hytale.server.core.util.BsonUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.bson.codecs.BsonDocumentCodecstatic final org.bson.json.JsonWriterSettings -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intencodeInto(org.bson.BsonDocument document, org.bson.io.BasicOutputBuffer buffer) Encodesdocumentinto the supplied (reusable) buffer using the default codec and returns the number of bytes written; a null document writes nothing and returns 0.static org.bson.BsonDocumentparseWithMaxDepth(String json) Parsesjsoninto aBsonDocumentlikeBsonDocument.parse(String), but rejects input nested deeper thanMAX_PARSE_DEPTHlevels so that deeply nested JSON cannot exhaust the JVM stack.static org.bson.BsonDocumentparseWithMaxDepth(String json, int maxDepth) Parsesjsoninto aBsonDocumentlikeparseWithMaxDepth(String), but with a caller-specifiedmaxDepthinstead ofMAX_PARSE_DEPTH.static CompletableFuture<org.bson.BsonDocument> readDocument(Path file) static CompletableFuture<org.bson.BsonDocument> readDocument(Path file, boolean backup) static CompletableFuture<org.bson.BsonDocument> readDocumentBak(Path fileOrig) static org.bson.BsonDocumentreadDocumentNow(Path file) static org.bson.BsonDocumentReads a BsonDocument from the specified ByteBuffer, consuming its length header and the BsonDocument's bytes.static org.bson.BsonDocumentreadFromBuffer(ByteBuffer buf) Reads a BsonDocument from the specified buffer using the default codec.static org.bson.BsonDocumentreadFromBytes(byte[] buf) Reads a BsonDocument from the specified byte array using the default codec.static StringtoJson(org.bson.BsonDocument document) static com.google.gson.JsonElementtranslateBsonToJson(org.bson.BsonDocument value) static org.bson.BsonValuetranslateJsonToBson(com.google.gson.JsonElement element) static CompletableFuture<Void> writeDocument(Path file, org.bson.BsonDocument document) Writesdocumenttofileas JSON, off the calling thread.static CompletableFuture<Void> writeDocument(Path file, org.bson.BsonDocument document, boolean backup) static <T> voidwriteSync(Path path, Codec<T> codec, T value, HytaleLogger logger) static voidwriteToBinaryStream(DataOutputStream out, org.bson.BsonDocument doc) static byte[]writeToBytes(org.bson.BsonDocument document) Writes the provided BsonDocument to a byte array using the default codec.
-
Field Details
-
SETTINGS
public static final org.bson.json.JsonWriterSettings SETTINGS -
BSON_DOCUMENT_CODEC
public static final org.bson.codecs.BsonDocumentCodec BSON_DOCUMENT_CODEC
-
-
Constructor Details
-
BsonUtil
public BsonUtil()
-
-
Method Details
-
writeToBytes
public static byte[] writeToBytes(@Nullable org.bson.BsonDocument document) Writes the provided BsonDocument to a byte array using the default codec. If a null document is provided, a 0-length byte array is returned.This method writes to the binary encoding of BSON.
- Parameters:
document- The document to write- Returns:
- The document encoded into a byte array, or an empty byte array if null was provided
-
encodeInto
public static int encodeInto(@Nullable org.bson.BsonDocument document, @Nonnull org.bson.io.BasicOutputBuffer buffer) Encodesdocumentinto the supplied (reusable) buffer using the default codec and returns the number of bytes written; a null document writes nothing and returns 0. The buffer is reset to position 0 before encoding, so the same thread-local buffer can be handed in repeatedly. UnlikewriteToBytes(BsonDocument), this allocates nobyte[]: the encoded bytes live inbuffer.getInternalBuffer()[0 .. returnValue]and can be copied straight into off-heap memory. The caller must consume the bytes before the nextencodeIntoon the same buffer.- Parameters:
document- the document to encode, ornullbuffer- a reusable output buffer (reset before encoding)- Returns:
- the number of bytes written
-
readFromBytes
@Nullable public static org.bson.BsonDocument readFromBytes(@Nullable byte[] buf) Reads a BsonDocument from the specified byte array using the default codec. If the byte array is null or 0-length, null is returned.This method reads from the binary encoding of BSON.
- Parameters:
buf- The byte array to parse- Returns:
- The BsonDocument, or null if the input was null or empty
-
readFromBuffer
Reads a BsonDocument from the specified buffer using the default codec. If the buffer is null or doesn't have any remaining bytes, null is returned.This method reads from the binary encoding of BSON.
- Parameters:
buf- The buffer to parse- Returns:
- The BsonDocument, or null if the input was null or empty
-
readFromBinaryStream
Reads a BsonDocument from the specified ByteBuffer, consuming its length header and the BsonDocument's bytes. Can return null if the originally encoded BsonDocument was null.This method reads from the binary encoding of BSON.
- Parameters:
buf- The ByteBuffer to read the BsonDocument from- Returns:
- The read BsonDocument, or null if the originally encoded BsonDocument was null or empty
-
writeToBinaryStream
public static void writeToBinaryStream(@Nonnull DataOutputStream out, org.bson.BsonDocument doc) throws IOException - Throws:
IOException
-
writeDocument
@Nonnull public static CompletableFuture<Void> writeDocument(@Nonnull Path file, org.bson.BsonDocument document) Writesdocumenttofileas JSON, off the calling thread.The caller gives up
document. The directory setup, the JSON encode and the file write all happen on another thread, so anything that mutates the document after this returns races the write.- Parameters:
file- The file to write.document- The document to write, which the caller must not touch afterwards.- Returns:
- A future that completes when the file has been written.
-
writeDocument
@Nonnull public static CompletableFuture<Void> writeDocument(@Nonnull Path file, org.bson.BsonDocument document, boolean backup) - Parameters:
backup- Whether to keep a backup of any file being replaced.- See Also:
-
readDocument
-
readDocument
@Nonnull public static CompletableFuture<org.bson.BsonDocument> readDocument(@Nonnull Path file, boolean backup) -
readDocumentNow
-
readDocumentBak
@Nonnull public static CompletableFuture<org.bson.BsonDocument> readDocumentBak(@Nonnull Path fileOrig) -
parseWithMaxDepth
Parsesjsoninto aBsonDocumentlikeBsonDocument.parse(String), but rejects input nested deeper thanMAX_PARSE_DEPTHlevels so that deeply nested JSON cannot exhaust the JVM stack. Use this in place ofBsonDocument.parsefor any client-supplied or otherwise untrusted input.- Throws:
DepthLimitedJsonReader.MaxDepthExceededException- if the nesting limit is exceeded
-
parseWithMaxDepth
Parsesjsoninto aBsonDocumentlikeparseWithMaxDepth(String), but with a caller-specifiedmaxDepthinstead ofMAX_PARSE_DEPTH.- Throws:
DepthLimitedJsonReader.MaxDepthExceededException- if the nesting limit is exceeded
-
translateJsonToBson
public static org.bson.BsonValue translateJsonToBson(@Nonnull com.google.gson.JsonElement element) -
translateBsonToJson
public static com.google.gson.JsonElement translateBsonToJson(org.bson.BsonDocument value) -
toJson
-
writeSync
public static <T> void writeSync(@Nonnull Path path, @Nonnull Codec<T> codec, T value, @Nonnull HytaleLogger logger) throws IOException - Throws:
IOException
-