Convert to and from UTF-8 without catching impossible exceptions.

All JVMs must support UTF-8 encoding.
This commit is contained in:
akwizgran
2014-02-07 22:02:02 +00:00
parent f6360c09d4
commit 4154119ea5
6 changed files with 15 additions and 19 deletions

View File

@@ -2,7 +2,7 @@ package org.briarproject.api;
import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset;
/** A pseudonym for a user. */ /** A pseudonym for a user. */
public class Author { public class Author {
@@ -14,13 +14,9 @@ public class Author {
private final byte[] publicKey; private final byte[] publicKey;
public Author(AuthorId id, String name, byte[] publicKey) { public Author(AuthorId id, String name, byte[] publicKey) {
if(name.length() == 0) throw new IllegalArgumentException(); int length = name.getBytes(Charset.forName("UTF-8")).length;
try { if(length == 0 || length > MAX_AUTHOR_NAME_LENGTH)
if(name.getBytes("UTF-8").length > MAX_AUTHOR_NAME_LENGTH) throw new IllegalArgumentException();
throw new IllegalArgumentException();
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
this.id = id; this.id = id;
this.name = name; this.name = name;
this.publicKey = publicKey; this.publicKey = publicKey;

View File

@@ -3,7 +3,7 @@ package org.briarproject.api.messaging;
import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH; import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH; import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH;
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset;
/** A group to which users may subscribe. */ /** A group to which users may subscribe. */
public class Group { public class Group {
@@ -13,13 +13,9 @@ public class Group {
private final byte[] salt; private final byte[] salt;
public Group(GroupId id, String name, byte[] salt) { public Group(GroupId id, String name, byte[] salt) {
if(name.length() == 0) throw new IllegalArgumentException(); int length = name.getBytes(Charset.forName("UTF-8")).length;
try { if(length == 0 || length > MAX_GROUP_NAME_LENGTH)
if(name.getBytes("UTF-8").length > MAX_GROUP_NAME_LENGTH) throw new IllegalArgumentException();
throw new IllegalArgumentException();
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
if(salt.length != GROUP_SALT_LENGTH) if(salt.length != GROUP_SALT_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.id = id; this.id = id;

View File

@@ -31,6 +31,7 @@ import org.briarproject.api.serial.DigestingConsumer;
import org.briarproject.api.serial.SigningConsumer; import org.briarproject.api.serial.SigningConsumer;
import org.briarproject.api.serial.Writer; import org.briarproject.api.serial.Writer;
import org.briarproject.api.serial.WriterFactory; import org.briarproject.api.serial.WriterFactory;
import org.briarproject.util.StringUtils;
class MessageFactoryImpl implements MessageFactory { class MessageFactoryImpl implements MessageFactory {
@@ -68,7 +69,7 @@ class MessageFactoryImpl implements MessageFactory {
// Validate the arguments // Validate the arguments
if((author == null) != (privateKey == null)) if((author == null) != (privateKey == null))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(contentType.getBytes("UTF-8").length > MAX_CONTENT_TYPE_LENGTH) if(StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(body.length > MAX_BODY_LENGTH) if(body.length > MAX_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();

View File

@@ -21,6 +21,7 @@ import static org.briarproject.serial.Tag.TRUE;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -234,7 +235,7 @@ class ReaderImpl implements Reader {
if(length < 0 || length > maxLength) throw new FormatException(); if(length < 0 || length > maxLength) throw new FormatException();
if(length == 0) return ""; if(length == 0) return "";
readIntoBuffer(length, true); readIntoBuffer(length, true);
return new String(buf, 0, length, "UTF-8"); return new String(buf, 0, length, Charset.forName("UTF-8"));
} }
private int readStringLength(boolean consume) throws IOException { private int readStringLength(boolean consume) throws IOException {

View File

@@ -16,6 +16,7 @@ import static org.briarproject.serial.Tag.TRUE;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -102,7 +103,7 @@ class WriterImpl implements Writer {
} }
public void writeString(String s) throws IOException { public void writeString(String s) throws IOException {
byte[] b = s.getBytes("UTF-8"); byte[] b = s.getBytes(Charset.forName("UTF-8"));
if(b.length <= Byte.MAX_VALUE) { if(b.length <= Byte.MAX_VALUE) {
write(STRING_8); write(STRING_8);
write((byte) b.length); write((byte) b.length);

View File

@@ -307,6 +307,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
for(int i = 0; i < b.length; i++) { for(int i = 0; i < b.length; i++) {
line[lineLen] = b[i]; line[lineLen] = b[i];
if(b[i] == '\n') { if(b[i] == '\n') {
// FIXME: Use CharsetDecoder to catch invalid ASCII
String s = new String(line, 0, lineLen, "US-ASCII").trim(); String s = new String(line, 0, lineLen, "US-ASCII").trim();
lineLen = 0; lineLen = 0;
if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s); if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s);