Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PubSub API prototype and related classes #962

Merged
merged 10 commits into from
Apr 28, 2016
10 changes: 10 additions & 0 deletions gcloud-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,15 @@
<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>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>0.0.11</version>
</dependency>
</dependencies>
</project>
23 changes: 23 additions & 0 deletions gcloud-java-core/src/main/java/com/google/cloud/AsyncPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2016 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 java.util.concurrent.Future;

public interface AsyncPage<T> extends Page<T> {
Future<AsyncPage<T>> nextPageAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.gax.grpc.ApiException;
import com.google.common.base.MoreObjects;

import java.io.IOException;
Expand Down Expand Up @@ -143,6 +144,16 @@ public BaseServiceException(int code, String message, String reason, boolean ide
this.debugInfo = null;
}

public BaseServiceException(ApiException apiException, boolean idempotent) {
super(apiException.getMessage(), apiException);
this.code = apiException.getStatusCode().value();
this.reason = apiException.getStatusCode().name();
this.idempotent = idempotent;
this.retryable = apiException.isRetryable();
this.location = null;
this.debugInfo = null;
}

protected Set<Error> retryableErrors() {
return Collections.emptySet();
}
Expand Down
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());
}
}
6 changes: 6 additions & 0 deletions gcloud-java-pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<artifactId>gcloud-java-pubsub</artifactId>
<packaging>jar</packaging>
<name>GCloud Java Pub/Sub</name>
<url>https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-pubsub</url>
<description>
Java idiomatic client for Google Cloud Pub/Sub.
</description>
Expand All @@ -16,6 +17,11 @@
<site.installationModule>gcloud-java-pubsub</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
Expand Down
Loading