/*
 * 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.facebook.presto.orc;

import com.facebook.presto.orc.metadata.CompressionKind;

import java.util.Optional;

public interface OrcDecompressor
{
    static Optional<OrcDecompressor> createOrcDecompressor(OrcDataSourceId orcDataSourceId, CompressionKind compression, int bufferSize)
            throws OrcCorruptionException
    {
        switch (compression) {
            case NONE:
                return Optional.empty();
            case ZLIB:
                return Optional.of(new OrcZlibDecompressor(orcDataSourceId, bufferSize));
            case SNAPPY:
                return Optional.of(new OrcSnappyDecompressor(orcDataSourceId, bufferSize));
            case ZSTD:
                return Optional.of(new OrcZstdDecompressor(orcDataSourceId, bufferSize));
            default:
                throw new OrcCorruptionException(orcDataSourceId, "Unknown compression type: " + compression);
        }
    }

    int decompress(byte[] input, int offset, int length, OutputBuffer output)
            throws OrcCorruptionException;

    interface OutputBuffer
    {
        byte[] initialize(int size);

        byte[] grow(int size);
    }
}

@@ -650,256 +650,748 @@
 nd;%0A
-%0Aimport java.util.Optional;%0A%0Apublic interface OrcDecompressor%0A%7B%0A    static Optional%3COrcDecompressor%3E createOrcDecompressor(OrcDataSourceId orcDataSourceId, CompressionKind compression, int bufferSize)%0A            throws OrcCorruptionException%0A    %7B
+import io.airlift.units.DataSize;%0A%0Aimport java.util.Optional;%0A%0Aimport static com.facebook.presto.orc.metadata.CompressionKind.NONE;%0Aimport static io.airlift.units.DataSize.Unit.MEGABYTE;%0Aimport static java.lang.Math.toIntExact;%0A%0Apublic interface OrcDecompressor%0A%7B%0A    int MAX_BUFFER_SIZE = toIntExact(new DataSize(4, MEGABYTE).toBytes());%0A%0A    static Optional%3COrcDecompressor%3E createOrcDecompressor(OrcDataSourceId orcDataSourceId, CompressionKind compression, int bufferSize)%0A            throws OrcCorruptionException%0A    %7B%0A        if ((compression != NONE) && ((bufferSize %3C= 0) %7C%7C (bufferSize %3E MAX_BUFFER_SIZE))) %7B%0A            throw new OrcCorruptionException(orcDataSourceId, %22Invalid compression block size: %22 + bufferSize);%0A        %7D
 %0A   

Validate ORC compression block size