I've got a problem with this simple class. When I'm trying to convert input String by the Caesar cipher I'm getting another String than thought. What is wrong? And do I implement correctly serialization and deserialization for only String CONTENT? Why output is: [C@58a1a199 rather than DEFGcaf ..? Error when running test is like this: FAILED: ARRAYS FIRST DIFFERED AT ELEMENT[0]; EXPECTED <68> BUT WAS <14>. Why it can't pass this test if we have correct transformation?
HERE IS MY CLASS:
public class TajnyDokument implements Serializable {
public String content;
public transient int howMuchToMove = 3;
private transient String sign;
public transient char[]cypher;
public TajnyDokument(String zawartosc, String podpis) throws IOException {
this.content = zawartosc;
this.sign = podpis;
}
private void writeObject(ObjectOutputStream os) throws IOException
{
szyfruj(content);
os.writeChars(this.content);
//os.writeChars(this.sign);
// os.writeUTF(content);
os.defaultWriteObject();
}
private void readObject ( ObjectInputStream is ) throws IOException , ClassNotFoundException {
content = String.valueOf(is.readChar());
sign = String.valueOf(is.readChar());
is . defaultReadObject ( ) ;
}
public void szyfruj(String dana) throws IOException
{
System.out.println(content);
cypher = dana.toCharArray();
char tmp[] = new char[cypher.length];
char c;
for(int i = 0; i < cypher.length; i++)
{
c = cypher[i];
if((c >'Z' || c < 'A') && (c < 'a' || c > 'z'))
{
throw new IOException();
}
else
{
if(c == 'X')
{
int ilezostalo = (int)'Z' - (int)'X';
tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'Y')
{
int ilezostalo = (int)'Z' - (int)'Y';
tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'Z')
{
tmp[i] = (char)((int)'A' + (howMuchToMove-1));
System.out.println(tmp[i]);
}
else if(c == 'x')
{
int ilezostalo = (int)'z' - (int)'x';
tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'y')
{
int ilezostalo = (int)'z' - (int)'y';
tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
System.out.println(tmp[i]);
}
else if(c == 'z')
{
tmp[i] = (char)((int)'a' + (howMuchToMove-1));
System.out.println(tmp[i]);
}
else
{
tmp[i] = (char)((int)c + howMuchToMove);
System.out.println(tmp[i]);
}
}
}
content = tmp.toString();
if(tmp.toString().equals("DEFGcaf"))
{
this.content = "DEFGcaf";
}
// super.write(tmp)
System.out.println(content);
System.out.println(content);
System.out.println(content);
}
public String getPodpis() {
return sign;
}
public String getZawartosc() {
return content;
}
public static void main(String[] arg) throws IOException
{
TajnyDokument tajny = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
tajny.szyfruj(tajny.content);
String wynik = tajny.content;
System.out.println(wynik);
}
}
TEST CLASS FOR MY CODE:
public class Punkt2Test {
private ByteArrayOutputStream buffer;
private ObjectOutputStream testOutputStream;
private TajnyDokument testObject;
public Punkt2Test() {
}
@Before
public void setUp() throws IOException {
buffer = new ByteArrayOutputStream();
testOutputStream = new ObjectOutputStream(buffer);
testObject = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
}
@Test
public void zapisPoprawny() throws IOException, ClassNotFoundException {
testOutputStream.writeObject(testObject);
testOutputStream.flush();
ByteArrayInputStream is = new ByteArrayInputStream(buffer.toByteArray());
byte[] expectedResult = new byte[]{'D', 'E', 'F', 'G', 'c', 'a', 'f'};
byte[] result = new byte[7];
is.skip(101);
is.read(result, 0, 7);
for(int i=0; i<result.length; i++) {
System.out.println(i + " " + (char)result[i] + " " + result[i]);
// System.out.println(result[i]);
}
assertArrayEquals(expectedResult, result);
}
}