1- /*
2- * Licensed to the Apache Software Foundation (ASF) under one or more
3- * contributor license agreements. See the NOTICE file distributed with this
4- * work for additional information regarding copyright ownership. The ASF
5- * licenses this file to You under the Apache License, Version 2.0 (the
6- * "License"); you may not use this file except in compliance with the License.
7- * You may obtain a copy of the License at
8- *
9- * http://www.apache.org/licenses/LICENSE-2.0
10- *
11- * Unless required by applicable law or agreed to in writing, software
12- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14- * License for the specific language governing permissions and limitations
15- * under the License.
16- */
171// Copyright 2017 JanusGraph Authors
182//
193// Licensed under the Apache License, Version 2.0 (the "License");
3014
3115package org .apache .hugegraph .computer .core .util ;
3216
33- import java .io . UnsupportedEncodingException ;
17+ import java .nio . charset . StandardCharsets ;
3418import java .security .MessageDigest ;
3519import java .security .NoSuchAlgorithmException ;
3620import java .util .Base64 ;
4428
4529/**
4630 * @author Matthias Broecheler ([email protected] ) 47- * @author HugeGraph Authors
4831 */
4932public final class StringEncoding {
5033
@@ -63,7 +46,7 @@ public final class StringEncoding {
6346 private static final Base64 .Encoder BASE64_ENCODER = Base64 .getEncoder ();
6447 private static final Base64 .Decoder BASE64_DECODER = Base64 .getDecoder ();
6548
66- // Similar to {@link StringSerializer}
49+ /** Similar to {@link StringSerializer} */
6750 public static int writeAsciiString (byte [] array , int offset , String value ) {
6851 E .checkArgument (CharMatcher .ascii ().matchesAllOf (value ),
6952 "'%s' must be ASCII string" , value );
@@ -79,7 +62,8 @@ public static int writeAsciiString(byte[] array, int offset, String value) {
7962 assert c <= 127 ;
8063 byte b = (byte ) c ;
8164 if (++i == len ) {
82- b |= 0x80 ; // End marker
65+ // End marker
66+ b |= 0x80 ;
8367 }
8468 array [offset ++] = b ;
8569 } while (i < len );
@@ -89,7 +73,7 @@ public static int writeAsciiString(byte[] array, int offset, String value) {
8973
9074 public static String readAsciiString (byte [] array , int offset ) {
9175 StringBuilder sb = new StringBuilder ();
92- int c = 0 ;
76+ int c ;
9377 do {
9478 c = 0xFF & array [offset ++];
9579 if (c != 0x80 ) {
@@ -106,27 +90,15 @@ public static int getAsciiByteLength(String value) {
10690 }
10791
10892 public static byte [] encode (String value ) {
109- try {
110- return value .getBytes ("UTF-8" );
111- } catch (UnsupportedEncodingException e ) {
112- throw new ComputerException ("Failed to encode string" , e );
113- }
93+ return value .getBytes (StandardCharsets .UTF_8 );
11494 }
11595
11696 public static String decode (byte [] bytes ) {
117- try {
118- return new String (bytes , "UTF-8" );
119- } catch (UnsupportedEncodingException e ) {
120- throw new ComputerException ("Failed to decode string" , e );
121- }
97+ return new String (bytes , StandardCharsets .UTF_8 );
12298 }
12399
124100 public static String decode (byte [] bytes , int offset , int length ) {
125- try {
126- return new String (bytes , offset , length , "UTF-8" );
127- } catch (UnsupportedEncodingException e ) {
128- throw new ComputerException ("Failed to decode string" , e );
129- }
101+ return new String (bytes , offset , length , StandardCharsets .UTF_8 );
130102 }
131103
132104 public static String encodeBase64 (byte [] bytes ) {
@@ -157,8 +129,7 @@ public static UUID uuid(String value) {
157129 return UUID .fromString (value );
158130 }
159131 // UUID represented by hex string
160- E .checkArgument (value .length () == 32 ,
161- "Invalid UUID string: %s" , value );
132+ E .checkArgument (value .length () == 32 , "Invalid UUID string: %s" , value );
162133 String high = value .substring (0 , 16 );
163134 String low = value .substring (16 );
164135 return new UUID (Long .parseUnsignedLong (high , 16 ),
0 commit comments