...

/

Utility Methods

Utility Methods

Write utility methods for repetitive tasks in the gRPC server and client.

Server-side utility methods

We will create utility methods for performing common tasks such as string validation and checksum matching. By specifying these utility methods in a separate class, we can conveniently reuse them throughout the server implementation without duplicating the code.

We will create a class ServerUtils in the utils package in src/main/java/io/datajek/ftpservice package in the ftp-service module. We will call the file validation method isNullOrEmpty and checksum matching method doChecksumsMatch as shown below.

Press + to interact
package io.datajek.ftpservice.utils;
public class ServerUtils {
// File validation method: isNullOrEmpty
//Checksum matching method: doChecksumsMatch
}

Method for validating file name

The isNullOrEmpty method will check if a given string is either empty or null. It will return true if the string is empty or null, and false otherwise. We will use this method to validate the file name during file transfer.

static public boolean isNullOrEmpty(String string) {
return "".equals(string) || string == null;
}
Validating file name

To check if the string is an empty string, the equals method in line 2 compares the content of the string to an empty string "". If they match, it means the string is empty.

To check if the string is null, we use == operator. If string is null, it means there is no memory location allocated for it.

The || operator is a logical OR operator, so if either of these conditions is true, the entire expression will evaluate to true, indicating that the string is either empty or null. Otherwise, if both conditions are false, it means the string is neither empty nor null, and the method will return false.

To summarize the logic:

  • If string is an empty string (""), the method returns true.

  • If string is null, the method returns true.

  • If string is neither an empty string nor null, the method returns false.

Method for matching checksum

In the doChecksumsMatch method, we will compare the hash of a given file chunk (as a ByteString) with the hash value received from the client. We will use the MD5 hash algorithm to compute the hash of the received content. If the computed hash and the received hash match, the method will return true. This utility method verifies the integrity of the file chunks during file transfer.

The doChecksumsMatch method takes two parameters of type ByteString named content and receivedHash. The method will return a Boolean value of true if the checksums match, and false otherwise. The method can throw a NoSuchAlgorithmException if the algorithm specified for message digest (MD5 in this case) is not available.

public static boolean doChecksumsMatch(ByteString content, ByteString receivedHash)
throws NoSuchAlgorithmException {
byte[] computedHash = MessageDigest.getInstance("MD5").digest(content.toByteArray());
return Arrays.equals(computedHash, receivedHash.toByteArray());
}
Checksum matching

Inside the method, we will calculate the hash (checksum) of the content using the MD5 algorithm as follows:

  • MessageDigest.getInstance("MD5"): We will obtain an instance of the MD5 message digest algorithm. It is used for calculating the hash.

  • .digest(content.toByteArray()): We will then calculate the hash of the content. The content.toByteArray() converts the content from a ByteString to a byte array, which is then passed to the MD5 message digest to compute the hash. The result is stored in the computedHash variable.

    • The digest method is used to ...