Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
use ByteString in codec api
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwawi committed Apr 12, 2016
1 parent deb273a commit 91eeb6a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ private <E> Either<Response<?>, E> deserialize(RequestContext rc, Class<? extend

final E entity;
try {
final ByteString byteString = payloadOpt.get();
entity = codec.read(byteString.toByteArray(), entityClass);
entity = codec.read(payloadOpt.get(), entityClass);
} catch (IOException e) {
LOG.warn("error", e);
return Either.left(Response.forStatus(
Expand All @@ -197,7 +196,7 @@ private <R> Function<Response<R>, Response<ByteString>> serialize(Class<? extend

final ByteString bytes;
try {
bytes = ByteString.of(codec.write(entityOpt.get(), entityClass));
bytes = codec.write(entityOpt.get(), entityClass);
} catch (IOException e) {
LOG.error("error", e);
return Response.forStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

import java.io.IOException;

import okio.ByteString;

/**
* Interface for serializing and de-serializing entity types.
*/
public interface EntityCodec {

String defaultContentType();

<E> byte[] write(E entity, Class<? extends E> clazz) throws IOException;
<E> ByteString write(E entity, Class<? extends E> clazz) throws IOException;

<E> E read(byte[] data, Class<? extends E> clazz) throws IOException;
<E> E read(ByteString data, Class<? extends E> clazz) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.IOException;
import java.util.Objects;

import okio.ByteString;

/**
* Codec for writing and reading values using a Jackson {@link ObjectMapper}.
*/
Expand All @@ -43,12 +45,12 @@ public String defaultContentType() {
}

@Override
public <E> byte[] write(E entity, Class<? extends E> clazz) throws IOException {
return objectMapper.writeValueAsBytes(entity);
public <E> ByteString write(E entity, Class<? extends E> clazz) throws IOException {
return ByteString.of(objectMapper.writeValueAsBytes(entity));
}

@Override
public <E> E read(byte[] data, Class<? extends E> clazz) throws IOException {
return objectMapper.readValue(data, clazz);
public <E> E read(ByteString data, Class<? extends E> clazz) throws IOException {
return objectMapper.readValue(data.toByteArray(), clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,22 @@ public String defaultContentType() {
}

@Override
public <E> byte[] write(E entity, Class<? extends E> clazz) throws IOException {
public <E> ByteString write(E entity, Class<? extends E> clazz) throws IOException {
if (!String.class.equals(clazz)) {
throw new UnsupportedOperationException("Can only encode strings");
}

return ((String) entity).getBytes();
return ByteString.encodeUtf8((String) entity);
}

@Override
public <E> E read(byte[] data, Class<? extends E> clazz) throws IOException {
public <E> E read(ByteString data, Class<? extends E> clazz) throws IOException {
if (!String.class.equals(clazz)) {
throw new UnsupportedOperationException("Can only encode strings");
}

//noinspection unchecked
return (E) new String(data);
return (E) data.utf8();
}
}
}

0 comments on commit 91eeb6a

Please sign in to comment.