Added logging
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 01-09-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
|
||||
|
||||
public class BaseX{
|
||||
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
|
||||
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private int base; //The base that the number will be encoded at
|
||||
@@ -23,6 +28,8 @@ public class BaseX{
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
@@ -34,15 +41,27 @@ public class BaseX{
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||
|
||||
//Create a string of valid 'numbers'
|
||||
logger.debug("Creating string of valid 'numbers'");
|
||||
StringBuilder validNumbers = new StringBuilder();
|
||||
for(int cnt = 0;cnt < base;++cnt){
|
||||
validNumbers.append(Integer.toString(cnt, base).toUpperCase());
|
||||
String number = Integer.toString(cnt, base).toUpperCase();
|
||||
logger.debug("Current number {}, converted {}", cnt, number);
|
||||
validNumbers.append(number);
|
||||
}
|
||||
|
||||
//Remove all invalid characters
|
||||
logger.debug("Checking for invalid characters");
|
||||
this.inputString = inputString.replaceAll("[^" + validNumbers.toString() + "\\s]", "");
|
||||
//Throw an exception if there were any invalid characters
|
||||
if(!this.inputString.equals(inputString)){
|
||||
throw new InvalidCharacterException("inputString cannot contain anything except numbers 0-" + Integer.toString(base - 1, base) + ", and whitespace");
|
||||
}
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
@@ -53,32 +72,47 @@ public class BaseX{
|
||||
throw new InvalidBaseException("Base cannot be a negative number");
|
||||
}
|
||||
|
||||
logger.debug("Setting base {}", base);
|
||||
|
||||
this.base = base;
|
||||
}
|
||||
//Encode inputString, store it in outputString, and return it
|
||||
private String encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Encode every character in inputString
|
||||
StringJoiner output = new StringJoiner(" ");
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
//Get the next character
|
||||
char ch = inputString.charAt(cnt);
|
||||
logger.debug("Working number {}", ch);
|
||||
|
||||
//Encode the character to binary and add it to the output
|
||||
output.add(Integer.toString(ch, base));
|
||||
String convertedNum = Integer.toString(ch, base);
|
||||
output.add(convertedNum);
|
||||
logger.debug("Converted number {}", convertedNum);
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString().toUpperCase();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
|
||||
//Return the output
|
||||
return outputString;
|
||||
}
|
||||
//Decode inputString, store it in outputString, and return it
|
||||
private String decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decode every binary number in the string
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(String baseXString : inputString.split(" ")){
|
||||
logger.debug("Current number {}", baseXString);
|
||||
|
||||
//Decode the current binary number
|
||||
int num = Integer.valueOf(baseXString, base);
|
||||
logger.debug("Decoded number {}", num);
|
||||
|
||||
//Make sure it is in a valid range
|
||||
if((num < 0) && (num > 255)){
|
||||
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character");
|
||||
@@ -89,6 +123,7 @@ public class BaseX{
|
||||
}
|
||||
|
||||
//Save the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
|
||||
//Return the output
|
||||
@@ -142,6 +177,8 @@ public class BaseX{
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user