Skip to content

Commit

Permalink
OAK-11377: Remove usage of Guava ByteStreams.toByteArray() (#1971)
Browse files Browse the repository at this point in the history
* OAK-11377: Remove usage of Guava ByteStreams.toByteArray()

* OAK-11377: Remove usage of Guava ByteStreams.toByteArray() - avoid deprecated method

* Revert "OAK-11377: Remove usage of Guava ByteStreams.toByteArray() - avoid deprecated method"

This reverts commit d210344.

(as this change is beyond the scope of this PR)
  • Loading branch information
reschke authored Jan 10, 2025
1 parent 746ff99 commit 9111252
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jackrabbit.oak.security.authentication.ldap;

import org.apache.jackrabbit.guava.common.io.ByteStreams;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.Appender;

Expand Down Expand Up @@ -47,10 +44,14 @@ public class LdapServerClassLoader extends URLClassLoader {

private LdapServerClassLoader(URL[] urls, Class serverClass, Class serverBaseClass) throws IOException {
super(urls, ClassLoader.getSystemClassLoader().getParent());
this.serverClassResource = ByteStreams.toByteArray(
serverClass.getResourceAsStream("/".concat(serverClass.getCanonicalName()).replace('.', '/').concat(".class")));
this.serverBaseClassResource = ByteStreams.toByteArray(
serverBaseClass.getResourceAsStream("/".concat(serverBaseClass.getCanonicalName()).replace('.', '/').concat(".class")));
this.serverClassResource = classAsBytes(serverClass);
this.serverBaseClassResource = classAsBytes(serverBaseClass);
}

private static byte[] classAsBytes(Class base) throws IOException {
try (InputStream is = base.getResourceAsStream("/".concat(base.getCanonicalName()).replace('.', '/').concat(".class"))) {
return is.readAllBytes();
}
}

public static LdapServerClassLoader createServerClassLoader() throws URISyntaxException, ClassNotFoundException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.NoSuchElementException;
import java.util.Objects;

import org.apache.jackrabbit.guava.common.io.ByteStreams;
import org.apache.jackrabbit.oak.commons.StringUtils;
import org.apache.jackrabbit.oak.plugins.blob.CachingBlobStore;
import org.jclouds.ContextBuilder;
Expand Down Expand Up @@ -160,7 +159,7 @@ protected byte[] readBlockFromBackend(BlockId blockId) throws Exception {

Payload payload = cloudBlob.getPayload();
try {
data = ByteStreams.toByteArray(payload.getInput());
data = payload.getInput().readAllBytes();
cache.put(id, data);
} finally {
payload.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.jackrabbit.core.data.DataStoreException;
import org.junit.Test;

import static org.apache.jackrabbit.guava.common.io.ByteStreams.toByteArray;
import static org.apache.jackrabbit.oak.plugins.blob.datastore.DataStoreUtils.randomStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void testMultiPartDirectUploadIT() throws DataRecordUploadException, Data
assertNotNull(retrievedRecord);

in.reset();
assertTrue(Arrays.equals(toByteArray(in), toByteArray(retrievedRecord.getStream())));
assertTrue(Arrays.equals(in.readAllBytes(), retrievedRecord.getStream().readAllBytes()));
}
finally {
if (null != uploadedRecord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

import org.apache.jackrabbit.guava.common.base.Strings;

import static org.apache.jackrabbit.guava.common.io.ByteStreams.toByteArray;
import static org.apache.jackrabbit.oak.plugins.blob.datastore.DataStoreUtils.randomStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -165,7 +164,7 @@ record = doSynchronousAddRecord((DataStore) dataStore, testStream);
assertEquals(200, conn.getResponseCode());

testStream.reset();
assertTrue(Arrays.equals(toByteArray(testStream), toByteArray(conn.getInputStream())));
assertTrue(Arrays.equals(testStream.readAllBytes(), conn.getInputStream().readAllBytes()));
}
finally {
if (null != record) {
Expand Down Expand Up @@ -240,7 +239,7 @@ record = doSynchronousAddRecord((DataStore) dataStore, testStream);
);

testStream.reset();
assertTrue(Arrays.equals(toByteArray(testStream), toByteArray(conn.getInputStream())));
assertTrue(Arrays.equals(testStream.readAllBytes(), conn.getInputStream().readAllBytes()));
}
}
finally {
Expand Down Expand Up @@ -586,7 +585,7 @@ public void testSinglePutDirectUploadIT() throws DataRecordUploadException, Data
DataRecord retrievedRecord = doGetRecord((DataStore) ds, uploadedRecord.getIdentifier());
assertNotNull(retrievedRecord);
uploadStream.reset();
assertTrue(Arrays.equals(toByteArray(uploadStream), toByteArray(retrievedRecord.getStream())));
assertTrue(Arrays.equals(uploadStream.readAllBytes(), retrievedRecord.getStream().readAllBytes()));
}
finally {
if (null != uploadedRecord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import org.apache.jackrabbit.guava.common.io.ByteStreams;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.api.PropertyState;
Expand Down Expand Up @@ -218,8 +217,8 @@ public void compactionNoBinaryClone() throws Exception {
long size7 = fileStore.getStats().getApproximateSize();

// No data loss
byte[] blob = ByteStreams.toByteArray(nodeStore.getRoot()
.getProperty("blob2").getValue(Type.BINARY).getNewStream());
byte[] blob = nodeStore.getRoot()
.getProperty("blob2").getValue(Type.BINARY).getNewStream().readAllBytes();
assertEquals(blobSize, blob.length);
} finally {
fileStore.close();
Expand Down Expand Up @@ -308,8 +307,8 @@ public void offlineCompaction() throws Exception {
assertTrue("the blob should not be collected", size7 > blobSize);

// No data loss
byte[] blob = ByteStreams.toByteArray(nodeStore.getRoot()
.getProperty("blob2").getValue(Type.BINARY).getNewStream());
byte[] blob = nodeStore.getRoot()
.getProperty("blob2").getValue(Type.BINARY).getNewStream().readAllBytes();
assertEquals(blobSize, blob.length);
} finally {
fileStore.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.jackrabbit.guava.common.io.ByteStreams;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
Expand Down Expand Up @@ -201,7 +200,7 @@ private static class Benchmark {

static {
try {
DATA = ByteStreams.toByteArray(new RandomStream(BLOB_SIZE, 100));
DATA = (new RandomStream(BLOB_SIZE, 100)).readAllBytes();
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Objects;
import java.util.StringJoiner;

import org.apache.jackrabbit.guava.common.io.ByteStreams;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
Expand Down Expand Up @@ -538,7 +537,7 @@ public NodeBuilder removeProperty(String name) {
@Override
public Blob createBlob(InputStream stream) throws IOException {
try {
return new ArrayBasedBlob(ByteStreams.toByteArray(stream));
return new ArrayBasedBlob(stream.readAllBytes());
} finally {
stream.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.jackrabbit.guava.common.io.ByteStreams;

import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
Expand Down Expand Up @@ -182,7 +180,7 @@ public NodeState reset(@NotNull NodeBuilder builder) {
@Override
public ArrayBasedBlob createBlob(InputStream inputStream) throws IOException {
try {
return new ArrayBasedBlob(ByteStreams.toByteArray(inputStream));
return new ArrayBasedBlob(inputStream.readAllBytes());
}
finally {
inputStream.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.Calendar;
import java.util.TimeZone;

import org.apache.jackrabbit.guava.common.io.ByteStreams;

import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.plugins.memory.StringBasedBlob;
Expand Down Expand Up @@ -156,14 +154,8 @@ public static Converter convert(final Blob value) {
return new Converter() {
@Override
public String toString() {
try {
InputStream in = value.getNewStream();
try {
return new String(ByteStreams.toByteArray(in), StandardCharsets.UTF_8);
}
finally {
in.close();
}
try (InputStream in = value.getNewStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
catch (IOException e) {
throw new IllegalArgumentException(e);
Expand Down

0 comments on commit 9111252

Please sign in to comment.