<commit_before>/**
 * Copyright 2012 <NAME>
 * This project includes software developed by <NAME>: https://github.com/jeluard/
 *
 * 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 com.github.jeluard.stone.spi;

import com.github.jeluard.guayaba.base.Pair;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 */
public abstract class BaseBinaryStorage extends BaseStorage {

  protected final void put(final long timestamp, final ByteBuffer buffer) {
    buffer.putLong(timestamp);
  }

  protected final long getTimestamp(final byte[] buffer) {
    return ByteBuffer.wrap(buffer).getLong();
  }

  protected final void put(final int[] ints, final ByteBuffer buffer) {
    for (final int i : ints) {
      buffer.putInt(i);
    }
  }

  protected final int[] getConsolidates(final byte[] buffer) {
    final int count = buffer.length/4;
    final int[] ints = new int[count];
    final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
    for (int i = 0; i < count; i++) {
      ints[i] = byteBuffer.getInt();
    }
    return ints;
  }

  protected final int bits2Bytes(final int bits) {
    return bits / Byte.SIZE;
  }

  protected ByteBuffer createByteBuffer(final int capacity) {
    return ByteBuffer.allocate(capacity);
  }

  @Override
  public void append(final long timestamp, final int[] consolidates) throws IOException {
    final int capacity = bits2Bytes(Long.SIZE) + bits2Bytes(Integer.SIZE) * consolidates.length;
    final ByteBuffer buffer = createByteBuffer(capacity);
    put(timestamp, buffer);
    put(consolidates, buffer);

    append(buffer);
  }

  protected abstract void append(ByteBuffer buffer) throws IOException;

}<commit_msg>Added some comments.<commit_after>/**
 * Copyright 2012 <NAME>
 * This project includes software developed by <NAME>: https://github.com/jeluard/
 *
 * 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 com.github.jeluard.stone.spi;

import com.github.jeluard.guayaba.base.Pair;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 */
public abstract class BaseBinaryStorage extends BaseStorage {

  /**
   * Put {@code timestamp} into {@code buffer}.
   *
   * @param ints
   * @param buffer 
   */
  protected final void put(final long timestamp, final ByteBuffer buffer) {
    buffer.putLong(timestamp);
  }

  /**
   * @param buffer
   * @return a {@link Long} composed of {@code buffer} content 
   */
  protected final long getTimestamp(final byte[] buffer) {
    assert buffer.length > 8;
    return ByteBuffer.wrap(buffer).getLong();
  }

  /**
   * Put all {@code ints} into {@code buffer}.
   *
   * @param ints
   * @param buffer 
   */
  protected final void put(final int[] ints, final ByteBuffer buffer) {
    for (final int i : ints) {
      buffer.putInt(i);
    }
  }

  /**
   * @param buffer
   * @return all {@link int[]} composed of {@code buffer} content 
   */
  protected final int[] getConsolidates(final byte[] buffer) {
    final int count = buffer.length/4;
    final int[] ints = new int[count];
    final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
    for (int i = 0; i < count; i++) {
      ints[i] = byteBuffer.getInt();
    }
    return ints;
  }

  /**
   * @param bits
   * @return convert number of bits into number of bytes
   */
  protected final int bits2Bytes(final int bits) {
    return bits / Byte.SIZE;
  }

  /**
   * @param capacity
   * @return a {@link ByteBuffer} with {@code capacity} capacity
   */
  protected ByteBuffer createByteBuffer(final int capacity) {
    return ByteBuffer.allocate(capacity);
  }

  @Override
  public final void append(final long timestamp, final int[] consolidates) throws IOException {
    final int capacity = bits2Bytes(Long.SIZE) + bits2Bytes(Integer.SIZE) * consolidates.length;
    final ByteBuffer buffer = createByteBuffer(capacity);
    put(timestamp, buffer);
    put(consolidates, buffer);

    append(buffer);
  }

  /**
   * Append the content of this {@link ByteBuffer} to the {@link Storage}.
   *
   * @param buffer
   * @throws IOException 
   */
  protected abstract void append(ByteBuffer buffer) throws IOException;

}
{'fraction_non_alphanumeric': 0.10479158550837553, 'fraction_numerical': 0.005259057265290222, 'mean_word_length': 3.434369602763385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '1566330', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1441, 'n_words': 517}