diff --git a/java/core/src/java/org/apache/orc/impl/TreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/TreeReaderFactory.java index ecc02fb8de..2a2adf50d7 100644 --- a/java/core/src/java/org/apache/orc/impl/TreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/TreeReaderFactory.java @@ -2292,10 +2292,15 @@ private void readDictionaryStream(InStream in) throws IOException { int dictionaryBufferSize = dictionaryOffsets[dictionaryOffsets.length - 1]; dictionaryBuffer = new byte[dictionaryBufferSize]; int pos = 0; - int chunkSize = in.available(); - byte[] chunkBytes = new byte[chunkSize]; + // check if dictionary size is smaller than available stream size + // to avoid ArrayIndexOutOfBoundsException + int readSize = Math.min(in.available(), dictionaryBufferSize); + byte[] chunkBytes = new byte[readSize]; while (pos < dictionaryBufferSize) { - int currentLength = in.read(chunkBytes, 0, chunkSize); + int currentLength = in.read(chunkBytes, 0, readSize); + // check if dictionary size is smaller than available stream size + // to avoid ArrayIndexOutOfBoundsException + currentLength = Math.min(currentLength, dictionaryBufferSize - pos); System.arraycopy(chunkBytes, 0, dictionaryBuffer, pos, currentLength); pos += currentLength; }