Skip to content

Commit

Permalink
add ByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov authored and mziccard committed Apr 27, 2016
1 parent 0b918ba commit fb6562e
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 14 deletions.
5 changes: 5 additions & 0 deletions gcloud-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,10 @@
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0-beta-1</version>
</dependency>
</dependencies>
</project>
165 changes: 165 additions & 0 deletions gcloud-java-core/src/main/java/com/google/cloud/ByteArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.protobuf.ByteString;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
* An immutable byte array holder.
*/
public class ByteArray implements Iterable<Byte>, Serializable {

private static final long serialVersionUID = -1908809133893782840L;
private final ByteString byteString;

protected ByteArray(ByteString byteString) {
this.byteString = byteString;
}

protected ByteArray(ByteArray byteArray) {
this.byteString = byteArray.byteString();
}

@Override
public final Iterator<Byte> iterator() {
return byteString.iterator();
}

@Override
public String toString() {
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
StringBuilder stBuilder = new StringBuilder();
for (int i = 0; i < Math.min(256, byteString.size()); i++) {
stBuilder.append(String.format("%02x", byteString.byteAt(i)));
}
if (byteString.size() > 256) {
stBuilder.append("...");
}
return toStringHelper.add("bytes", stBuilder.toString()).toString();
}

@Override
public final int hashCode() {
return byteString.hashCode();
}

@Override
public final boolean equals(Object obj) {
return obj == this
|| obj instanceof ByteArray && byteString.equals(((ByteArray) obj).byteString);
}

/**
* Returns the size of this blob.
*/
public final int length() {
return byteString.size();
}

/**
* Returns a copy as byte array.
*/
public final byte[] toByteArray() {
return byteString.toByteArray();
}

/**
* Returns the content as {@code UTF-8} string.
*/
public final String toStringUtf8() {
return byteString.toStringUtf8();
}

/**
* Returns a read-only {@link ByteBuffer} for this blob content.
*/
public final ByteBuffer asReadOnlyByteBuffer() {
return byteString.asReadOnlyByteBuffer();
}

/**
* Returns an {@link InputStream} for this blob content.
*/
public final InputStream asInputStream() {
final ByteBuffer byteBuffer = asReadOnlyByteBuffer();
return new InputStream() {
@Override public int read() {
return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF;
}
};
}

protected ByteString byteString() {
return byteString;
}

/**
* Copies bytes into a ByteBuffer.
*
* @throws java.nio.ReadOnlyBufferException if the target is read-only
* @throws java.nio.BufferOverflowException if the target's remaining() space is not large
* enough to hold the data
*/
public final void copyTo(ByteBuffer target) {
byteString.copyTo(target);
}

/**
* Copies bytes into a buffer.
*
* @throws IndexOutOfBoundsException if an offset or size is negative or too large
*/
public final void copyTo(byte[] target) {
byteString.copyTo(target, 0, 0, length());
}

public static final ByteArray copyFrom(byte[] bytes) {
return new ByteArray(ByteString.copyFrom(bytes));
}

/**
* Copy the bytes using {@code UTF-8} decoding.
*/
public static final ByteArray copyFrom(String string) {
return new ByteArray(ByteString.copyFrom(string, StandardCharsets.UTF_8));
}

public static final ByteArray copyFrom(ByteBuffer bytes) {
return new ByteArray(ByteString.copyFrom(bytes));
}

public static final ByteArray copyFrom(InputStream input) throws IOException {
BufferedInputStream bufferedInput = new BufferedInputStream(input);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
int value;
while ((value = bufferedInput.read()) != -1) {
bytes.write(value);
}
return copyFrom(bytes.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.ByteArray;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
Expand All @@ -37,18 +38,37 @@ public class Message implements Serializable {
private static final long MILLIS_PER_SECOND = 1000;

private final String id;
private final ByteString payload;
private final InternalByteArray payload;
private final ImmutableMap<String, String> attributes;
private final Long publishTime;

private static final class InternalByteArray extends ByteArray {

private static final long serialVersionUID = -3330181485911805428L;

protected InternalByteArray(ByteString byteString) {
super(byteString);
}

protected InternalByteArray(ByteArray byteArray) {
super(byteArray);
}

protected ByteString byteString() {
return super.byteString();
}
}

/**
* Builder for Message.
*/
public abstract static class Builder {

abstract Builder id(String id);

public abstract Builder payload(byte[] payload);
public abstract Builder payload(String payload);

public abstract Builder payload(ByteArray payload);

public abstract Builder attributes(Map<String, String> attributes);

Expand All @@ -66,15 +86,15 @@ public abstract static class Builder {
static final class BuilderImpl extends Builder {

private String id = "";
private byte[] payload;
private ByteArray payload;
private Map<String, String> attributes = new HashMap<>();
private Long publishTime;

private BuilderImpl() {}

BuilderImpl(Message message) {
id = message.id;
payload = message.payload.toByteArray();
payload = message.payload;
attributes = new HashMap<>(message.attributes);
publishTime = message.publishTime;
}
Expand All @@ -86,8 +106,13 @@ BuilderImpl id(String id) {
}

@Override
public Builder payload(byte[] payload) {
this.payload = checkNotNull(payload);
public Builder payload(String payload) {
return payload(ByteArray.copyFrom(payload));
}

@Override
public Builder payload(ByteArray payload) {
this.payload = payload;
return this;
}

Expand Down Expand Up @@ -129,7 +154,7 @@ public Message build() {

Message(BuilderImpl builder) {
id = builder.id;
payload = ByteString.copyFrom(checkNotNull(builder.payload));
payload = new InternalByteArray(checkNotNull(builder.payload));
attributes = ImmutableMap.copyOf(builder.attributes);
publishTime = builder.publishTime;
}
Expand All @@ -146,8 +171,12 @@ public String id() {
return id;
}

public byte[] payload() {
return payload.toByteArray();
public String payloadAsString() {
return payload.toStringUtf8();
}

public ByteArray payload() {
return payload;
}

@Override
Expand Down Expand Up @@ -181,7 +210,7 @@ PubsubMessage toPb() {
if (id != null) {
builder.setMessageId(id);
}
builder.setData(payload);
builder.setData(payload.byteString());
builder.getAttributes().putAll(attributes);
Timestamp.Builder tsBuilder = Timestamp.newBuilder();
tsBuilder.setSeconds(publishTime / MILLIS_PER_SECOND);
Expand All @@ -191,7 +220,7 @@ PubsubMessage toPb() {
}

static Message fromPb(PubsubMessage messagePb) {
Builder builder = builder(messagePb.getData().toByteArray());
Builder builder = builder(new InternalByteArray(messagePb.getData()));
if (messagePb.hasPublishTime()) {
Timestamp ts = messagePb.getPublishTime();
builder.publishTime(
Expand All @@ -208,11 +237,15 @@ public Builder toBuilder() {
return new BuilderImpl(this);
}

public static Message of(byte[] payload) {
public static Message of(String payload) {
return builder(payload).build();
}

public static Builder builder(byte[] payload) {
public static Builder builder(String payload) {
return new BuilderImpl().payload(payload);
}

public static Builder builder(ByteArray payload) {
return new BuilderImpl().payload(payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.ByteArray;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;
Expand Down Expand Up @@ -55,7 +57,13 @@ Builder id(String id) {
}

@Override
public Builder payload(byte[] payload) {
public Builder payload(String payload) {
delegate.payload(payload);
return this;
}

@Override
public Builder payload(ByteArray payload) {
delegate.payload(payload);
return this;
}
Expand Down

0 comments on commit fb6562e

Please sign in to comment.