
com.hazelcast.shaded.nonapi.io.github.classgraph.fileslice.ArraySlice Maven / Gradle / Ivy
The newest version!
/*
* This file is part of ClassGraph.
*
* Author: Luke Hutchison
*
* Hosted at: https://github.com/classgraph/classgraph
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Luke Hutchison
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.hazelcast.shaded.nonapi.io.github.classgraph.fileslice;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import com.hazelcast.shaded.nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler;
import com.hazelcast.shaded.nonapi.io.github.classgraph.fileslice.reader.RandomAccessArrayReader;
import com.hazelcast.shaded.nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader;
/** A byte array slice. */
public class ArraySlice extends Slice {
/** The wrapped byte array. */
public byte[] arr;
/**
* Constructor for treating a range of an array as a slice.
*
* @param parentSlice
* the parent slice
* @param offset
* the offset of the sub-slice within the parent slice
* @param length
* the length of the sub-slice
* @param isDeflatedZipEntry
* true if this is a deflated zip entry
* @param inflatedLengthHint
* the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated
* zip entry.
* @param nestedJarHandler
* the nested jar handler
*/
private ArraySlice(final ArraySlice parentSlice, final long offset, final long length,
final boolean isDeflatedZipEntry, final long inflatedLengthHint,
final NestedJarHandler nestedJarHandler) {
super(parentSlice, offset, length, isDeflatedZipEntry, inflatedLengthHint, nestedJarHandler);
this.arr = parentSlice.arr;
}
/**
* Constructor for treating a whole array as a slice.
*
* @param arr
* the array containing the slice.
* @param isDeflatedZipEntry
* true if this is a deflated zip entry
* @param inflatedLengthHint
* the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated
* zip entry.
* @param nestedJarHandler
* the nested jar handler
*/
public ArraySlice(final byte[] arr, final boolean isDeflatedZipEntry, final long inflatedLengthHint,
final NestedJarHandler nestedJarHandler) {
super(arr.length, isDeflatedZipEntry, inflatedLengthHint, nestedJarHandler);
this.arr = arr;
}
/**
* Slice this slice to form a sub-slice.
*
* @param offset
* the offset relative to the start of this slice to use as the start of the sub-slice.
* @param length
* the length of the sub-slice.
* @param isDeflatedZipEntry
* the is deflated zip entry
* @param inflatedLengthHint
* the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated
* zip entry.
* @return the slice
*/
@Override
public Slice slice(final long offset, final long length, final boolean isDeflatedZipEntry,
final long inflatedLengthHint) {
if (this.isDeflatedZipEntry) {
throw new IllegalArgumentException("Cannot slice a deflated zip entry");
}
return new ArraySlice(this, offset, length, isDeflatedZipEntry, inflatedLengthHint, nestedJarHandler);
}
/**
* Load the slice as a byte array.
*
* @return the byte[]
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Override
public byte[] load() throws IOException {
if (isDeflatedZipEntry) {
// Deflate into RAM if necessary
try (InputStream inputStream = open()) {
return NestedJarHandler.readAllBytesAsArray(inputStream, inflatedLengthHint);
}
} else if (sliceStartPos == 0L && sliceLength == arr.length) {
// Fast path -- return whole array, if the array is the whole slice and is not deflated
return arr;
} else {
// Copy range of array, if it is a slice and it is not deflated
return Arrays.copyOfRange(arr, (int) sliceStartPos, (int) (sliceStartPos + sliceLength));
}
}
/**
* Return a new random access reader.
*
* @return the random access reader
*/
@Override
public RandomAccessReader randomAccessReader() {
return new RandomAccessArrayReader(arr, (int) sliceStartPos, (int) sliceLength);
}
@Override
public boolean equals(final Object o) {
return super.equals(o);
}
@Override
public int hashCode() {
return super.hashCode();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy