Skip to content

Commit

Permalink
NIFI-14138 Replaced deprecated com.google.common.base.Charsets with J…
Browse files Browse the repository at this point in the history
…ava 21 equivalent java.nio.charset.StandardCharsets

Signed-off-by: Pierre Villard <[email protected]>

This closes #9615.
  • Loading branch information
dan-s1 authored and pvillard31 committed Jan 9, 2025
1 parent 9ae65cc commit b80a673
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.apache.nifi.processors.evtx.parser;

import com.google.common.base.Charsets;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
Expand Down Expand Up @@ -59,23 +59,23 @@ public void testPeek() throws IOException {

@Test
public void testReadBytesJustLength() throws IOException {
byte[] bytes = "Hello world".getBytes(Charsets.US_ASCII);
byte[] bytes = "Hello world".getBytes(StandardCharsets.US_ASCII);
BinaryReader binaryReader = testBinaryReaderBuilder.put(bytes).build();
assertArrayEquals(Arrays.copyOfRange(bytes, 0, 5), binaryReader.readBytes(5));
assertEquals(5, binaryReader.getPosition());
}

@Test
public void testPeekBytes() throws IOException {
byte[] bytes = "Hello world".getBytes(Charsets.US_ASCII);
byte[] bytes = "Hello world".getBytes(StandardCharsets.US_ASCII);
BinaryReader binaryReader = testBinaryReaderBuilder.put(bytes).build();
assertArrayEquals(Arrays.copyOfRange(bytes, 0, 5), binaryReader.peekBytes(5));
assertEquals(0, binaryReader.getPosition());
}

@Test
public void testReadBytesBufOffsetLength() throws IOException {
byte[] bytes = "Hello world".getBytes(Charsets.US_ASCII);
byte[] bytes = "Hello world".getBytes(StandardCharsets.US_ASCII);
byte[] buf = new byte[5];

BinaryReader binaryReader = testBinaryReaderBuilder.put(bytes).build();
Expand All @@ -96,7 +96,7 @@ public void testReadGuid() throws IOException {
public void testReadStringNotNullTerminated() throws IOException {
String value = "Hello world";

BinaryReader binaryReader = testBinaryReaderBuilder.put(value.getBytes(Charsets.US_ASCII)).build();
BinaryReader binaryReader = testBinaryReaderBuilder.put(value.getBytes(StandardCharsets.US_ASCII)).build();
assertThrows(IOException.class, () -> binaryReader.readString(value.length()));
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public void testReadFileTIme() throws IOException {
@Test
public void testReadAndBase64EncodeBinary() throws IOException {
String orig = "Hello World";
String stringValue = Base64.getEncoder().encodeToString(orig.getBytes(Charsets.US_ASCII));
String stringValue = Base64.getEncoder().encodeToString(orig.getBytes(StandardCharsets.US_ASCII));
BinaryReader binaryReader = testBinaryReaderBuilder.putBase64EncodedBinary(stringValue).build();

assertEquals(stringValue, binaryReader.readAndBase64EncodeBinary(orig.length()));
Expand All @@ -192,7 +192,7 @@ public void testSkip() throws IOException {
@Test
public void testReaderPositionConstructor() throws IOException {
String value = "Hello world";
BinaryReader binaryReader = new BinaryReader(new BinaryReader(value.getBytes(Charsets.UTF_16LE)), 2);
BinaryReader binaryReader = new BinaryReader(new BinaryReader(value.getBytes(StandardCharsets.UTF_16LE)), 2);

assertEquals(value.substring(1), binaryReader.readWString(value.length() - 1));
assertEquals(value.length() * 2, binaryReader.getPosition());
Expand All @@ -201,7 +201,7 @@ public void testReaderPositionConstructor() throws IOException {
@Test
public void testInputStreamSizeConstructor() throws IOException {
String value = "Hello world";
BinaryReader binaryReader = new BinaryReader(new ByteArrayInputStream(value.getBytes(Charsets.UTF_16LE)), 10);
BinaryReader binaryReader = new BinaryReader(new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_16LE)), 10);

assertEquals(value.substring(0, 5), binaryReader.readWString(5));
assertEquals(10, binaryReader.getPosition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.apache.nifi.processors.evtx.parser;

import com.google.common.base.Charsets;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Calendar;
Expand Down Expand Up @@ -63,13 +63,13 @@ public TestBinaryReaderBuilder putGuid(String guid) {
}

public TestBinaryReaderBuilder putString(String val) {
data.add(val.getBytes(Charsets.US_ASCII));
data.add(val.getBytes(StandardCharsets.US_ASCII));
data.add(new byte[]{0});
return this;
}

public TestBinaryReaderBuilder putWString(String val) {
data.add(val.getBytes(Charsets.UTF_16LE));
data.add(val.getBytes(StandardCharsets.UTF_16LE));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package org.apache.nifi.processors.evtx.parser.bxml.value;

import com.google.common.base.Charsets;
import com.google.common.primitives.UnsignedInteger;
import org.apache.nifi.processors.evtx.parser.BinaryReader;
import org.apache.nifi.processors.evtx.parser.bxml.BxmlNodeTestBase;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -34,20 +34,21 @@ public class BinaryTypeNodeTest extends BxmlNodeTestBase {
public void testLength() throws IOException {
String val = "Test String";
BinaryReader binaryReader = testBinaryReaderBuilder.putDWord(val.length()).putString(val).build();
assertEquals(Base64.getEncoder().encodeToString(val.getBytes(Charsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, -1).getValue());
assertEquals(Base64.getEncoder().encodeToString(val.getBytes(StandardCharsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, -1).getValue());
}

@Test
public void testInvalidStringLength() throws IOException {
String val = "Test String";
BinaryReader binaryReader = testBinaryReaderBuilder.putDWord(UnsignedInteger.fromIntBits(Integer.MAX_VALUE + 1)).putString(val).build();
assertThrows(IOException.class, () -> assertEquals(Base64.getEncoder().encodeToString(val.getBytes(Charsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, -1).getValue()));
assertThrows(IOException.class,
() -> assertEquals(Base64.getEncoder().encodeToString(val.getBytes(StandardCharsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, -1).getValue()));
}

@Test
public void testNoLength() throws IOException {
String val = "Test String";
BinaryReader binaryReader = testBinaryReaderBuilder.putString(val).build();
assertEquals(Base64.getEncoder().encodeToString(val.getBytes(Charsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, val.length()).getValue());
assertEquals(Base64.getEncoder().encodeToString(val.getBytes(StandardCharsets.US_ASCII)), new BinaryTypeNode(binaryReader, chunkHeader, parent, val.length()).getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.apache.nifi.processors.parquet;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setUp() throws Exception {
.set("mylong", 2L)
.set("myfloat", 3.1f)
.set("mydouble", 4.1)
.set("mybytes", ByteBuffer.wrap("hello".getBytes(Charsets.UTF_8)))
.set("mybytes", ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))
.set("mystring", "hello")
.set("mynestedrecord", nestedRecord)
.set("myarray", new GenericData.Array<>(Schema.createArray(Schema.create(Schema.Type.INT)), Arrays.asList(1, 2)))
Expand Down

0 comments on commit b80a673

Please sign in to comment.