Skip to content

Commit

Permalink
Enabled Base64 codec on grounds of previous implemented Base64 de/enc…
Browse files Browse the repository at this point in the history
…oder. Enabled tests as well.
  • Loading branch information
semantosoph committed May 23, 2017
1 parent 790ab2a commit 3d5e463
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
56 changes: 40 additions & 16 deletions src/main/java/org/takes/facets/auth/codecs/CcBase64.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,37 @@
package org.takes.facets.auth.codecs;

import lombok.EqualsAndHashCode;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.takes.facets.auth.Identity;
import org.takes.misc.Base64;

/**
* Base64 codec.
*
* <p>The class is immutable and thread-safe.
*
* @author Igor Khvostenkov (ikhvostenkov@gmail.com)
* @author Sven Windisch (sven.windisch@gmail.com)
* @version $Id$
* @since 0.13
*/
@EqualsAndHashCode
public final class CcBase64 implements Codec {

/**
* All legal Base64 chars.
*/
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

/**
* Original codec.
*/
private final Codec origin;

/**
* Ctor.
* @param codec Original codec
Expand All @@ -49,24 +63,34 @@ public CcBase64(final Codec codec) {
this.origin = codec;
}

//@todo #19:30min to implement own simple Base64 encode algorithm
// without using 3d-party Base64 encode libraries. Tests for this
// method have been already created, do not forget to remove Ignore
// annotation on it.
@Override
public byte[] encode(final Identity identity) {
assert this.origin != null;
throw new UnsupportedOperationException("#encode()");
public byte[] encode(final Identity identity) throws IOException {
return new Base64().encode(this.origin.encode(identity));
}

//@todo #19:30min to implement own simple Base64 decode algorithm
// without using 3d-party Base64 decode libraries. Tests for this
// method have been already created, do not forget to remove Ignore
// annotation on it.
@Override
public Identity decode(final byte[] bytes) {
assert this.origin != null;
throw new UnsupportedOperationException("#decode()");
public Identity decode(final byte[] bytes) throws IOException {
final byte[] illegal = CcBase64.checkIllegalCharacters(bytes);
if (illegal.length > 0) {
throw new DecodingException("Illegal character in Base64 encoded data. " + Arrays.toString(illegal));
}
return this.origin.decode(new Base64().decode(bytes));
}

}

/**
* Check the byte array for non-Base64 characters.
*
* @param bytes The values to check
* @return An array of the found non-Base64 characters.
*/
private static byte[] checkIllegalCharacters(byte[] bytes) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();

for (int c = 0; c < bytes.length; c++) {
if (base64chars.indexOf(bytes[c]) < 0) {
out.write(bytes[c]);
}
}
return out.toByteArray();
}
}
3 changes: 1 addition & 2 deletions src/test/java/org/takes/facets/auth/codecs/CcBase64Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@
import nl.jqno.equalsverifier.Warning;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.takes.facets.auth.Identity;
import org.takes.misc.Base64;

/**
* Test case for {@link CcBase64}.
* @author Igor Khvostenkov (ikhvostenkov@gmail.com)
* @version $Id$
* @since 0.13
*/
@Ignore
public final class CcBase64Test {
/**
* CcBase64 can encode.
Expand Down

0 comments on commit 3d5e463

Please sign in to comment.