Class BsonUtil

java.lang.Object
com.hypixel.hytale.server.core.util.BsonUtil

public class BsonUtil extends Object
  • 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)
      Encodes document into 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. Unlike writeToBytes(BsonDocument), this allocates no byte[]: the encoded bytes live in buffer.getInternalBuffer()[0 .. returnValue] and can be copied straight into off-heap memory. The caller must consume the bytes before the next encodeInto on the same buffer.
      Parameters:
      document - the document to encode, or null
      buffer - 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

      @Nullable public static org.bson.BsonDocument readFromBuffer(@Nullable ByteBuffer buf)
      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

      @Nullable public static org.bson.BsonDocument readFromBinaryStream(@Nonnull ByteBuffer buf)
      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)
      Writes document to file as 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

      @Nonnull public static CompletableFuture<org.bson.BsonDocument> readDocument(@Nonnull Path file)
    • readDocument

      @Nonnull public static CompletableFuture<org.bson.BsonDocument> readDocument(@Nonnull Path file, boolean backup)
    • readDocumentNow

      @Nullable public static org.bson.BsonDocument readDocumentNow(@Nonnull Path file)
    • readDocumentBak

      @Nonnull public static CompletableFuture<org.bson.BsonDocument> readDocumentBak(@Nonnull Path fileOrig)
    • parseWithMaxDepth

      @Nonnull public static org.bson.BsonDocument parseWithMaxDepth(@Nonnull String json)
      Parses json into a BsonDocument like BsonDocument.parse(String), but rejects input nested deeper than MAX_PARSE_DEPTH levels so that deeply nested JSON cannot exhaust the JVM stack. Use this in place of BsonDocument.parse for any client-supplied or otherwise untrusted input.
      Throws:
      DepthLimitedJsonReader.MaxDepthExceededException - if the nesting limit is exceeded
    • parseWithMaxDepth

      @Nonnull public static org.bson.BsonDocument parseWithMaxDepth(@Nonnull String json, int maxDepth)
      Parses json into a BsonDocument like parseWithMaxDepth(String), but with a caller-specified maxDepth instead of MAX_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

      public static String toJson(org.bson.BsonDocument document)
    • writeSync

      public static <T> void writeSync(@Nonnull Path path, @Nonnull Codec<T> codec, T value, @Nonnull HytaleLogger logger) throws IOException
      Throws:
      IOException