Skip to content

Commit

Permalink
refactor(core): StringEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
diaohancai committed Dec 27, 2023
1 parent 0500fec commit 59f12a3
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 179 deletions.
1 change: 0 additions & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ header: # `header` section is configurations for source codes license header.
- 'assembly/**'
- '.github/**/*'
- '**/target/*'
- '**/util/StringEncoding.java'
- '**/go.mod'
- '**/go.sum'
comment: on-failure # on what condition license-eye will comment on the pull request, `on-failure`, `always`, `never`.
Expand Down
9 changes: 0 additions & 9 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,3 @@
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.

========================================================================
Apache 2.0 licenses
========================================================================

The following components are provided under the Apache License. See project link for details.
The text of each license is the standard Apache 2.0 license.

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncoding.java files from https://github.com/JanusGraph
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.IOException;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.testutil.Whitebox;

@SuppressWarnings("deprecation") // StringEscapeUtils
Expand Down Expand Up @@ -67,14 +67,14 @@ public void write(int b) throws IOException {

@Override
public void write(byte[] b) throws IOException {
this.writeString(StringEncoding.encodeBase64(b));
this.writeString(StringEncodeUtil.encodeBase64(b));
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
byte[] dest = new byte[len];
System.arraycopy(b, off, dest, 0, len);
this.writeString(StringEncoding.encodeBase64(dest));
this.writeString(StringEncodeUtil.encodeBase64(dest));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.List;

import org.apache.hugegraph.computer.core.common.exception.ComputerException;
import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;
Expand Down Expand Up @@ -146,12 +146,12 @@ public static String readString(ByteBuf buf) {
}
byte[] bytes = new byte[length];
buf.readBytes(bytes);
return StringEncoding.decode(bytes);
return StringEncodeUtil.decode(bytes);
}

public static void writeString(ByteBuf buf, String value) {
E.checkArgumentNotNull(value, "value");
byte[] encoded = StringEncoding.encode(value);
byte[] encoded = StringEncodeUtil.encode(value);
buf.writeInt(encoded.length);
buf.writeBytes(encoded);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.hugegraph.computer.core.config.Config;
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
import org.apache.hugegraph.computer.core.output.AbstractComputerOutput;
import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;

Expand Down Expand Up @@ -93,7 +93,7 @@ protected void writeBytes(byte[] bytes) throws IOException {
}

protected void writeString(String string) throws IOException {
this.writeBytes(StringEncoding.encode(string));
this.writeBytes(StringEncodeUtil.encode(string));
}

protected String constructValueString(Vertex vertex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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 org.apache.hugegraph.computer.core.util;

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

public final class StringEncodeUtil {

Check warning on line 23 in computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java

View check run for this annotation

Codecov / codecov/patch

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java#L23

Added line #L23 was not covered by tests

private static final byte[] BYTES_EMPTY = new byte[0];

private static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder();
private static final Base64.Decoder BASE64_DECODER = Base64.getDecoder();

public static byte[] encode(String value) {
return value.getBytes(StandardCharsets.UTF_8);
}

public static String decode(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8);
}

public static String decode(byte[] bytes, int offset, int length) {
return new String(bytes, offset, length, StandardCharsets.UTF_8);

Check warning on line 39 in computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java

View check run for this annotation

Codecov / codecov/patch

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java#L39

Added line #L39 was not covered by tests
}

public static String encodeBase64(byte[] bytes) {
return BASE64_ENCODER.encodeToString(bytes);
}

public static byte[] decodeBase64(String value) {
if (value.isEmpty()) {
return BYTES_EMPTY;

Check warning on line 48 in computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java

View check run for this annotation

Codecov / codecov/patch

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java#L48

Added line #L48 was not covered by tests
}
return BASE64_DECODER.decode(value);

Check warning on line 50 in computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java

View check run for this annotation

Codecov / codecov/patch

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncodeUtil.java#L50

Added line #L50 was not covered by tests
}
}

This file was deleted.

2 changes: 0 additions & 2 deletions computer-dist/release-docs/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ The following components are provided under the Apache 2.0 License.
(Apache License, Version 2.0) * okio(com.squareup.okio:okio-jvm:3.0.0-https://github.com/square/okio/ )
(Apache License, Version 2.0) * Simple XML (safe)(com.carrotsearch.thirdparty:simple-xml-safe:2.7.1-https://github.com/dweiss/simplexml )

computer-core/src/main/java/org/apache/hugegraph/computer/core/util/StringEncoding.java files from https://github.com/JanusGraph

========================================================================
Third party BSD licenses
========================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.net.InetSocketAddress;

import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.testutil.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -88,7 +88,7 @@ public void testFormatAddress() {

@Test
public void testReadString() {
byte[] testData = StringEncoding.encode("test data");
byte[] testData = StringEncodeUtil.encode("test data");
ByteBuf buffer = Unpooled.directBuffer(testData.length);
try {
buffer.writeInt(testData.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.nio.ByteBuffer;

import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.testutil.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -100,7 +100,7 @@ public void testNettyByteBuffer() {
@Test
public void testCopyToByteArray() {
String testData = "test data";
byte[] bytesSource = StringEncoding.encode(testData);
byte[] bytesSource = StringEncodeUtil.encode(testData);

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bytesSource.length);
byteBuffer = byteBuffer.put(bytesSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.hugegraph.computer.core.network.message.MessageType;
import org.apache.hugegraph.computer.core.network.message.StartMessage;
import org.apache.hugegraph.computer.core.network.netty.codec.FrameDecoder;
import org.apache.hugegraph.computer.core.util.StringEncoding;
import org.apache.hugegraph.computer.core.util.StringEncodeUtil;
import org.apache.hugegraph.computer.suite.unit.UnitTestBase;
import org.apache.hugegraph.testutil.Assert;
import org.junit.Test;
Expand All @@ -52,7 +52,7 @@ public void testSendMsgWithEncoderExceptionMock() throws IOException {

int requestId = 1;
int partition = 1;
byte[] bytes = StringEncoding.encode("mock msg");
byte[] bytes = StringEncodeUtil.encode("mock msg");
ByteBuffer buffer = ByteBuffer.wrap(bytes);
NetworkBuffer body = new NioBuffer(buffer);
DataMessage dataMessage = new DataMessage(null, requestId,
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testClientDecodeException() throws IOException {
public void testMessageRelease() {
int requestId = 99;
int partition = 1;
byte[] bytes = StringEncoding.encode("mock msg");
byte[] bytes = StringEncodeUtil.encode("mock msg");
ByteBuf buf = Unpooled.directBuffer().writeBytes(bytes);
try {
NettyBuffer managedBuffer = new NettyBuffer(buf);
Expand Down
Loading

0 comments on commit 59f12a3

Please sign in to comment.