<commit_before>/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mongodb;

import org.bson.BSONBinaryReader;
import org.bson.BSONBinaryWriter;
import org.bson.io.BasicInputBuffer;
import org.bson.io.BasicOutputBuffer;
import org.mongodb.serialization.Serializer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * A simple wrapper around a byte array that is the representation of a single BSON document.
 */
// TODO: Add an easy way to iterate over the fields?
public class BSONDocumentBuffer {
    private final byte[] bytes;

    /**
     * Constructs a new instance with the given byte array.  Note that it does not make a copy of the array, so do not
     * modify it after passing it to this constructor.
     *
     * @param bytes the bytes representing a BSON document.
     */
    public BSONDocumentBuffer(final byte[] bytes) {
        if (bytes == null) {
            throw new IllegalArgumentException("bytes can not be null");
        }
        this.bytes = bytes;
    }

    /**
     * Construct a new instance from the given document and serializer for the document type.
     *
     * @param document   the document to transform
     * @param serializer the serializer to facilitate the transformation
     */
    public <T> BSONDocumentBuffer(final T document, final Serializer<T> serializer) {
        BSONBinaryWriter writer = new BSONBinaryWriter(new BasicOutputBuffer());<commit_msg>Added a comment about buffer ownership<commit_after>/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mongodb;

import org.bson.BSONBinaryReader;
import org.bson.BSONBinaryWriter;
import org.bson.io.BasicInputBuffer;
import org.bson.io.BasicOutputBuffer;
import org.mongodb.serialization.Serializer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * A simple wrapper around a byte array that is the representation of a single BSON document.
 */
// TODO: Add an easy way to iterate over the fields?
public class BSONDocumentBuffer {
    private final byte[] bytes;

    /**
     * Constructs a new instance with the given byte array.  Note that it does not make a copy of the array, so do not
     * modify it after passing it to this constructor.
     *
     * @param bytes the bytes representing a BSON document.  Note that the byte array is NOT copied, so care
     *              must be taken not to modify it after passing it to this construction, unless of course
     *              that is your intention.
     */
    public BSONDocumentBuffer(final byte[] bytes) {
        if (bytes == null) {
            throw new IllegalArgumentException("bytes can not be null");
        }
        this.bytes = bytes;
    }

    /**
     * Construct a new instance from the given document and serializer for the document type.
     *
     * @param document   the document to transform
     * @param serializer the serializer to facilitate the transformation
     */
    public <T> BSONDocumentBuffer(final T document, final Serializer<T> serializer) {
        BSONBinaryWriter writer = new BSONBinaryWriter(new BasicOutputBuffer());
{'fraction_non_alphanumeric': 0.07492318600803592, 'fraction_numerical': 0.007563223824155046, 'mean_word_length': 3.1984126984126986, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4258411', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1089, 'n_words': 496}