Skip to content

Commit

Permalink
Utf8String implements Cactoos Bytes and Text (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Dec 18, 2018
1 parent 990b232 commit ec5a55e
Show file tree
Hide file tree
Showing 45 changed files with 98 additions and 107 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/takes/facets/auth/PsBasic.java
Expand Up @@ -102,7 +102,7 @@ public Opt<Identity> enter(final Request request) throws IOException {
DatatypeConverter.parseBase64Binary(
PsBasic.AUTH.split(headers.next())[1]
)
).string().trim();
).asString().trim();
final String user = decoded.split(":")[0];
final Opt<Identity> identity = this.entry.enter(
user,
Expand Down Expand Up @@ -309,7 +309,7 @@ private Opt<String> urn(final String user, final String pwd) {
/**
* Creates a key for
* {@link org.takes.facets.auth.PsBasic.Default#usernames} map.
* @param unified User string made of 3 urlencoded substrings
* @param unified User asString made of 3 urlencoded substrings
* separated with non-urlencoded space characters.
* @return Login and password parts with <pre>%20</pre> replaced with
* <pre>+</pre>.
Expand All @@ -326,7 +326,7 @@ private static String key(final String unified) {
}

/**
* Checks if a unified user string is correctly formatted.
* Checks if a unified user asString is correctly formatted.
* @param unified String with urlencoded user login, password and urn
* separated with spaces.
*/
Expand All @@ -345,9 +345,9 @@ private static void validateUser(final String unified) {
}

/**
* Counts spaces in a string.
* @param txt Any string.
* @return Amount of spaces in string.
* Counts spaces in a asString.
* @param txt Any asString.
* @return Amount of spaces in asString.
*/
private static int countSpaces(final String txt) {
int spaces = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/auth/PsCookie.java
Expand Up @@ -100,7 +100,7 @@ public Opt<Identity> enter(final Request req) throws IOException {
if (cookies.hasNext()) {
user = new Opt.Single<>(
this.codec.decode(
new Utf8String(cookies.next()).bytes()
new Utf8String(cookies.next()).asBytes()
)
);
}
Expand All @@ -114,7 +114,7 @@ public Response exit(final Response res,
if (idt.equals(Identity.ANONYMOUS)) {
text = "";
} else {
text = new Utf8String(this.codec.encode(idt)).string();
text = new Utf8String(this.codec.encode(idt)).asString();
}
return new RsWithCookie(
res, this.cookie, text,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/RqAuth.java
Expand Up @@ -76,7 +76,7 @@ public Identity identity() throws IOException {
final Identity user;
if (headers.hasNext()) {
user = new CcPlain().decode(
new Utf8String(headers.next()).bytes()
new Utf8String(headers.next()).asBytes()
);
} else {
user = Identity.ANONYMOUS;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/RqWithAuth.java
Expand Up @@ -110,7 +110,7 @@ private static Request make(final Identity identity, final String header,
return new RqWithHeader(
req,
header,
new Utf8String(new CcPlain().encode(identity)).string()
new Utf8String(new CcPlain().encode(identity)).asString()
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/takes/facets/auth/codecs/CcAes.java
Expand Up @@ -132,7 +132,7 @@ public Identity decode(final byte[] bytes) throws IOException {
}

/**
* Encrypt the given bytes using AES.
* Encrypt the given asBytes using AES.
*
* @param bytes Bytes to encrypt
* @return Encrypted byte using AES algorithm
Expand Down Expand Up @@ -171,7 +171,7 @@ private static byte[] withCorrectBlockSize(final byte[] key) {
if (key.length != CcAes.BLOCK) {
throw new IllegalArgumentException(
String.format(
"the length of the AES key must be exactly %d bytes",
"the length of the AES key must be exactly %d asBytes",
CcAes.BLOCK
)
);
Expand All @@ -180,10 +180,10 @@ private static byte[] withCorrectBlockSize(final byte[] key) {
}

/**
* Decrypt the given bytes using AES.
* Decrypt the given asBytes using AES.
*
* @param bytes Bytes to decrypt
* @return Decrypted bytes
* @return Decrypted asBytes
* @throws IOException for all unexpected exceptions
*/
private byte[] decrypt(final byte[] bytes) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/auth/codecs/CcPlain.java
Expand Up @@ -57,12 +57,12 @@ public byte[] encode(final Identity identity) throws IOException {
.append('=')
.append(URLEncoder.encode(ent.getValue(), encoding));
}
return new Utf8String(text.toString()).bytes();
return new Utf8String(text.toString()).asBytes();
}

@Override
public Identity decode(final byte[] bytes) throws IOException {
final String[] parts = new Utf8String(bytes).string().split(";");
final String[] parts = new Utf8String(bytes).asString().split(";");
final Map<String, String> map = new HashMap<>(parts.length);
for (int idx = 1; idx < parts.length; ++idx) {
final String[] pair = parts[idx].split("=");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/takes/facets/auth/codecs/CcSalted.java
Expand Up @@ -69,9 +69,9 @@ public Identity decode(final byte[] bytes) throws IOException {
}

/**
* Salt the string.
* Salt the asString.
* @param text Original text to salt
* @return Salted string
* @return Salted asString
*/
@SuppressWarnings("PMD.AvoidArrayLoops")
private static byte[] salt(final byte[] text) {
Expand All @@ -89,7 +89,7 @@ private static byte[] salt(final byte[] text) {
}

/**
* Un-salt the string.
* Un-salt the asString.
* @param text Salted text
* @return Original text
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/auth/codecs/CcXor.java
Expand Up @@ -55,7 +55,7 @@ public final class CcXor implements Codec {
* @param key Secret key for encoding
*/
public CcXor(final Codec codec, final String key) {
this(codec, new Utf8String(key).bytes());
this(codec, new Utf8String(key).asBytes());
}

/**
Expand All @@ -79,7 +79,7 @@ public Identity decode(final byte[] bytes) throws IOException {
}

/**
* XOR array of bytes.
* XOR array of asBytes.
* @param input The input to XOR
* @return Encrypted output
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/codecs/Codec.java
Expand Up @@ -36,7 +36,7 @@
public interface Codec {

/**
* Encode identity into bytes.
* Encode identity into asBytes.
* @param identity The identity
* @return Text
* @throws IOException If fails
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/auth/signatures/SiHmac.java
Expand Up @@ -134,11 +134,11 @@ private static int bitLength(final int bits) {
}

/**
* Encrypt the given bytes using HMAC.
* Encrypt the given asBytes using HMAC.
*
* @param bytes
* Bytes to encrypt
* @return Encrypted bytes
* @return Encrypted asBytes
* @throws IOException
* for all unexpected exceptions
*/
Expand Down
Expand Up @@ -35,7 +35,7 @@
public interface Signature {

/**
* Create signature for data bytes.
* Create signature for data asBytes.
* @param data The data to be signed
* @return Signature
* @throws IOException If anything fails
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/social/PsTwitter.java
Expand Up @@ -194,7 +194,7 @@ private String fetch() throws IOException {
"Basic %s", DatatypeConverter.printBase64Binary(
new Utf8String(
String.format("%s:%s", this.app, this.key)
).bytes()
).asBytes()
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/cookies/RsWithCookie.java
Expand Up @@ -115,7 +115,7 @@ public RsWithCookie(final Response res, final CharSequence name,
}

/**
* Build cookie string.
* Build cookie asString.
* @param name Cookie name
* @param value Value of it
* @param attrs Optional attributes, for example "Path=/"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/flash/RsFlash.java
Expand Up @@ -106,7 +106,7 @@ public final class RsFlash extends RsWrap {
private static final String TEXT_FORMAT = "%s/%s";

/**
* To string.
* To asString.
*/
private final CharSequence text;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fork/FkEncoding.java
Expand Up @@ -48,7 +48,7 @@
* new FkEncoding("", response)
* )</pre>
*
* <p>Empty string as an encoding means that the fork should match
* <p>Empty asString as an encoding means that the fork should match
* in any case.
*
* <p>The class is immutable and thread-safe.
Expand All @@ -64,7 +64,7 @@ public final class FkEncoding implements Fork {
private static final Pattern ENCODING_SEP = Pattern.compile("\\s*,\\s*");

/**
* Encoding we can deliver (or empty string).
* Encoding we can deliver (or empty asString).
*/
private final String encoding;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/fork/RqRegex.java
Expand Up @@ -41,7 +41,7 @@
public interface RqRegex extends Request {

/**
* Get matcher of query string.
* Get matcher of query asString.
* @return Matcher
*/
Matcher matcher();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/hamcrest/HmBody.java
Expand Up @@ -147,7 +147,7 @@ private static InputStream itemBody(final Body item) throws IOException {
}

/**
* InputStream as bytes.
* InputStream as asBytes.
* @param input Input
* @return Bytes
* @throws IOException If some problem inside
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/hamcrest/HmHeader.java
Expand Up @@ -46,7 +46,7 @@
public final class HmHeader<T extends Head> extends TypeSafeMatcher<T> {

/**
* Values string used in description of mismatches.
* Values asString used in description of mismatches.
*/
private static final String VALUES_STR = " -> values: ";

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/MainRemote.java
Expand Up @@ -155,7 +155,7 @@ private static int port(final File file) throws Exception {
break;
}
}
port = Integer.parseInt(new Utf8String(buf).string().trim());
port = Integer.parseInt(new Utf8String(buf).asString().trim());
}
return port;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/takes/misc/EnglishLowerCase.java
Expand Up @@ -26,7 +26,7 @@
import java.util.Locale;

/**
* English lower case string representation.
* English lower case asString representation.
* @since 0.33
*/
public final class EnglishLowerCase {
Expand All @@ -53,7 +53,7 @@ public EnglishLowerCase(final String string) {
}

/**
* Returns string value.
* Returns asString value.
* @return String value
*/
public String string() {
Expand Down
25 changes: 8 additions & 17 deletions src/main/java/org/takes/misc/Utf8String.java
Expand Up @@ -24,17 +24,14 @@
package org.takes.misc;

import java.nio.charset.Charset;
import org.cactoos.Bytes;
import org.cactoos.Text;

/**
* String that uses UTF-8 encoding for all byte operations.
* @since 0.33
* @todo #804:30min Make this implements Cactoos Bytes and Text
* and make the places in Takes that use it be able to directly
* take either a Bytes or a Text depending on the situation.
* For example RsWithBody should take Bytes and Text in its constructors.
* Also reimplement this class with BytesOf.
*/
public final class Utf8String {
public final class Utf8String implements Bytes, Text {

/**
* UTF-8 encoding.
Expand All @@ -56,25 +53,19 @@ public Utf8String(final String string) {

/**
* Ctor.
* @param bytes Bytes to construct UTF-8 string value
* @param bytes Bytes to construct UTF-8 asString value
*/
public Utf8String(final byte... bytes) {
this(new String(bytes, Charset.forName(Utf8String.ENCODING)));
}

/**
* Encodes string value into a sequence of bytes using UTF-8 charset.
* @return Sequence of bytes
*/
public byte[] bytes() {
@Override
public byte[] asBytes() {
return this.value.getBytes(Charset.forName(Utf8String.ENCODING));
}

/**
* Returns string value.
* @return String value
*/
public String string() {
@Override
public String asString() {
return this.value;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/CapInputStream.java
Expand Up @@ -41,7 +41,7 @@ final class CapInputStream extends InputStream {
private final InputStream origin;

/**
* More bytes to read.
* More asBytes to read.
*/
private long more;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/ChunkedInputStream.java
Expand Up @@ -199,7 +199,7 @@ private enum State {
*/
R,
/**
* Inside quoted string.
* Inside quoted asString.
*/
QUOTED_STRING,
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqFake.java
Expand Up @@ -89,7 +89,7 @@ public RqFake(final CharSequence method, final CharSequence query,
* @param body Body
*/
public RqFake(final List<String> head, final CharSequence body) {
this(head, new Utf8String(body.toString()).bytes());
this(head, new Utf8String(body.toString()).asBytes());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqLengthAware.java
Expand Up @@ -39,7 +39,7 @@
* most cases, the browser will not close the request and will always
* return positive number in available() method. Thus, you won't be
* able to reach the end of the stream ever. The browser wants you
* to respect the "Content-Length" header and read as many bytes
* to respect the "Content-Length" header and read as many asBytes
* as it requests. To solve that, just wrap your request into this
* decorator.
*
Expand Down

0 comments on commit ec5a55e

Please sign in to comment.