|
| 1 | +package com.google.gcloud.datastore; |
| 2 | + |
| 3 | +import static com.google.api.client.util.Preconditions.checkArgument; |
| 4 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | + |
| 6 | +import com.google.protobuf.ByteString; |
| 7 | + |
| 8 | +import java.io.BufferedInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.ByteBuffer; |
| 13 | +import java.util.Objects; |
| 14 | + |
| 15 | +/** |
| 16 | + * A Google Cloud Datastore Blob. |
| 17 | + * A Datastore blob is limited to {@value #MAX_LENGTH} bytes. |
| 18 | + * This class is immutable. |
| 19 | + * |
| 20 | + * @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a> |
| 21 | + */ |
| 22 | +public final class Blob implements java.io.Serializable { |
| 23 | + |
| 24 | + private static final long serialVersionUID = 3835421019618247721L; |
| 25 | + private static final int MAX_LENGTH = 1_000_000; |
| 26 | + |
| 27 | + private final ByteString byteString; |
| 28 | + |
| 29 | + Blob(ByteString byteString, boolean enforceLimits) { |
| 30 | + this.byteString = checkNotNull(byteString); |
| 31 | + if (enforceLimits) { |
| 32 | + checkArgument(byteString.size() <= MAX_LENGTH, "May be a maximum of 1,000,000 bytes"); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String toString() { |
| 38 | + return byteString.toString(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public int hashCode() { |
| 43 | + return byteString.hashCode(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean equals(Object obj) { |
| 48 | + if (!(obj instanceof Blob)) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + return Objects.equals(byteString, ((Blob) obj).byteString); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the size of this blob. |
| 56 | + */ |
| 57 | + public int length() { |
| 58 | + return byteString.size(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns a copy as byte array. |
| 63 | + */ |
| 64 | + public byte[] toByteArray() { |
| 65 | + return byteString.toByteArray(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a read-only {@link ByteBuffer} for this blob content. |
| 70 | + */ |
| 71 | + public ByteBuffer asReadOnlyByteBuffer() { |
| 72 | + return byteString.asReadOnlyByteBuffer(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns an {@link InputStream} for this blob content. |
| 77 | + */ |
| 78 | + public InputStream asInputStream() { |
| 79 | + final ByteBuffer byteBuffer = asReadOnlyByteBuffer(); |
| 80 | + return new InputStream() { |
| 81 | + @Override public int read() throws IOException { |
| 82 | + return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Copies bytes into a ByteBuffer. |
| 89 | + * |
| 90 | + * @throws java.nio.ReadOnlyBufferException if the target is read-only |
| 91 | + * @throws java.nio.BufferOverflowException if the target's remaining() space is not large |
| 92 | + * enough to hold the data. |
| 93 | + */ |
| 94 | + public void copyTo(ByteBuffer target) { |
| 95 | + byteString.copyTo(target); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Copies bytes into a buffer. |
| 100 | + * |
| 101 | + * @throws java.io.IndexOutOfBoundsException if an offset or size is negative or too large |
| 102 | + */ |
| 103 | + public void copyTo(byte[] target) { |
| 104 | + byteString.copyTo(target, 0, 0, length()); |
| 105 | + } |
| 106 | + |
| 107 | + ByteString byteString() { |
| 108 | + return byteString; |
| 109 | + } |
| 110 | + |
| 111 | + public static Blob copyFrom(byte[] bytes) { |
| 112 | + return new Blob(ByteString.copyFrom(bytes), true); |
| 113 | + } |
| 114 | + |
| 115 | + public static Blob copyFrom(ByteBuffer bytes) { |
| 116 | + return new Blob(ByteString.copyFrom(bytes), true); |
| 117 | + } |
| 118 | + |
| 119 | + public static Blob copyFrom(InputStream input) throws IOException { |
| 120 | + BufferedInputStream bufferedInput = new BufferedInputStream(input); |
| 121 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 122 | + int value; |
| 123 | + while ((value = bufferedInput.read()) != -1) { |
| 124 | + bytes.write(value); |
| 125 | + } |
| 126 | + return copyFrom(bytes.toByteArray()); |
| 127 | + } |
| 128 | +} |
0 commit comments