<commit_before>/*
 * Copyright Debezium Authors.
 *
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package io.debezium.connector.oracle.xstream;

import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import io.debezium.DebeziumException;
import oracle.streams.ChunkColumnValue;

/**
 * A simple wrapper class around a collection of {@link ChunkColumnValue}s.
 *
 * @author <NAME>
 */
public class ChunkColumnValues {

    private final List<ChunkColumnValue> values = new ArrayList<>();
    private long size = 0;

    /**
     * Gets the chunk type of the managed {@link ChunkColumnValue} instances.
     *
     * @return the chunk type of the values
     * @throws DebeziumException if the method is called before adding at least one ChunkColumnValue.
     */
    public int getChunkType() {
        if (values.isEmpty()) {
            throw new DebeziumException("Unable to resolve chunk type since no chunks have yet been added.");
        }
        return values.get(0).getChunkType();
    }

    /**
     * @return {@code true} if there are no values, {@code false} if at least one value has been added.
     */
    public boolean isEmpty() {
        return values.isEmpty();
    }

    /**
     * Adds a chunk column value instance to this collection.
     *
     * @param chunkColumnValue the chunk column value to be added
     */
    public void add(ChunkColumnValue chunkColumnValue) {
        size += calculateChunkSize(chunkColumnValue);
        values.add(chunkColumnValue);
    }

    /**
     * @return the chunk data as a string, may be {@code null} if the length of the data is zero.
     * @throws SQLException if there is a database exception accessing the raw chunk value
     */
    public String getStringValue() throws SQLException {
        if (size == 0) {
            return null;
        }
        StringBuilder data = new StringBuilder();
        for (ChunkColumnValue value : values) {
            data.append(value.getColumnData().stringValue());
        }
        return data.toString();
    }

    /**
     * @return the chunk data as a byte array, may be {@code null} if the length of the data is zero.
     * @throws SQLException if there is a database exception accessing the raw chunk value
     */
    public byte[] getByteArray() throws SQLException {
        if (size == 0) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate((int) size);
        for (ChunkColumnValue columnValue : values) {
            buffer.put(columnValue.getColumnData().getBytes());
        }
        return buffer.array();
    }

    /**
     * Calculates the size of the individual column chunk.
     *
     * @param chunkColumnValue a specific chunk of column data
     * @return the size of the column chunk data
     * @throws DebeziumException if there was a problem resolving the size of the column chunk data
     */
    private static int calculateChunkSize(ChunkColumnValue chunkColumnValue) {
        try {
            switch (chunkColumnValue.getChunkType()) {
                case ChunkColumnValue.CLOB:
                case ChunkColumnValue.NCLOB:
                    return chunkColumnValue.getColumnData().stringValue().length();
                case ChunkColumnValue.BLOB:
                    return chunkColumnValue.getColumnData().getBytes().length;
                default:
                    return 0;
            }
        }
        catch (SQLException e) {
            throw new DebeziumException("Failed to calculate the size of the chunk column data value");
        }
    }
}<commit_msg>DBZ-3631 Suggested changes & formatting fixes<commit_after>/*
 * Copyright Debezium Authors.
 *
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package io.debezium.connector.oracle.xstream;

import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import io.debezium.DebeziumException;

import oracle.streams.ChunkColumnValue;

/**
 * A simple wrapper class around a collection of {@link ChunkColumnValue}s.
 *
 * @author <NAME>
 */
public class ChunkColumnValues {

    private final List<ChunkColumnValue> values = new ArrayList<>();
    private long size = 0;

    /**
     * Gets the chunk type of the managed {@link ChunkColumnValue} instances.
     *
     * @return the chunk type of the values
     * @throws DebeziumException if the method is called before adding at least one ChunkColumnValue.
     */
    public int getChunkType() {
        if (values.isEmpty()) {
            throw new DebeziumException("Unable to resolve chunk type since no chunks have yet been added.");
        }
        return values.get(0).getChunkType();
    }

    /**
     * @return {@code true} if there are no values, {@code false} if at least one value has been added.
     */
    public boolean isEmpty() {
        return values.isEmpty();
    }

    /**
     * Adds a chunk column value instance to this collection.
     *
     * @param chunkColumnValue the chunk column value to be added
     */
    public void add(ChunkColumnValue chunkColumnValue) {
        size += calculateChunkSize(chunkColumnValue);
        values.add(chunkColumnValue);
    }

    /**
     * @return the chunk data as a string, may be {@code null} if the length of the data is zero.
     * @throws SQLException if there is a database exception accessing the raw chunk value
     */
    public String getStringValue() throws SQLException {
        if (size == 0) {
            return null;
        }
        StringBuilder data = new StringBuilder();
        for (ChunkColumnValue value : values) {
            data.append(value.getColumnData().stringValue());
        }
        return data.toString();
    }

    /**
     * @return the chunk data as a byte array, may be {@code null} if the length of the data is zero.
     * @throws SQLException if there is a database exception accessing the raw chunk value
     */
    public byte[] getByteArray() throws SQLException {
        if (size == 0) {
            return null;
        }
        if (size > Integer.MAX_VALUE) {
            throw new DebeziumException("Size " + size + " exceeds maximum value " + Integer.MAX_VALUE);
        }
        ByteBuffer buffer = ByteBuffer.allocate((int) size);
        for (ChunkColumnValue columnValue : values) {
            buffer.put(columnValue.getColumnData().getBytes());
        }
        return buffer.array();
    }

    /**
     * Calculates the size of the individual column chunk.
     *
     * @param chunkColumnValue a specific chunk of column data
     * @return the size of the column chunk data
     * @throws DebeziumException if there was a problem resolving the size of the column chunk data
     */
    private int calculateChunkSize(ChunkColumnValue chunkColumnValue) {
        try {
            switch (chunkColumnValue.getChunkType()) {
                case ChunkColumnValue.CLOB:
                case ChunkColumnValue.NCLOB:
                    return chunkColumnValue.getColumnData().stringValue().length();
                case ChunkColumnValue.BLOB:
                    return chunkColumnValue.getColumnData().getBytes().length;
                default:
                    return 0;
            }
        }
        catch (SQLException e) {
            throw new DebeziumException("Failed to calculate the size of the chunk column data value");
        }
    }
}
{'fraction_non_alphanumeric': 0.08744542928958857, 'fraction_numerical': 0.0029104378886096044, 'mean_word_length': 2.361493997332148, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '3146392', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1935, 'n_words': 686}