/** * Copies the given bytes into a {@code ByteString}. * * @param bytes to copy * @return new {@code ByteString} */ public static ByteString copyFrom(byte[] bytes) { return copyFrom(bytes, 0, bytes.length); }
/** * Copies the given bytes into a {@code ByteString}. * * @param bytes to copy * @return new {@code ByteString} */ public static ByteString copyFrom(byte[] bytes) { return copyFrom(bytes, 0, bytes.length); }
/** * Copies the given bytes into a {@code ByteString}. * * @param bytes to copy * @return new {@code ByteString} */ public static ByteString copyFrom(byte[] bytes) { return copyFrom(bytes, 0, bytes.length); }
/** * Copies the remaining bytes from a {@code java.nio.ByteBuffer} into * a {@code ByteString}. * * @param bytes sourceBuffer * @return new {@code ByteString} */ public static ByteString copyFrom(ByteBuffer bytes) { return copyFrom(bytes, bytes.remaining()); }
/** * Copies the remaining bytes from a {@code java.nio.ByteBuffer} into * a {@code ByteString}. * * @param bytes sourceBuffer * @return new {@code ByteString} */ public static ByteString copyFrom(ByteBuffer bytes) { return copyFrom(bytes, bytes.remaining()); }
/** * Copies the remaining bytes from a {@code java.nio.ByteBuffer} into * a {@code ByteString}. * * @param bytes sourceBuffer * @return new {@code ByteString} */ public static ByteString copyFrom(ByteBuffer bytes) { return copyFrom(bytes, bytes.remaining()); }
/** * Helper called by generated code to construct default values for bytes * fields. * <p> * This is a lot like {@link #stringDefaultValue}, but for bytes fields. * In this case we only need the second of the two hacks -- allowing us to * embed raw bytes as a string literal with ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { try { return ByteString.copyFrom(bytes.getBytes("ISO-8859-1")); } catch (UnsupportedEncodingException e) { // This should never happen since all JVMs are required to implement // ISO-8859-1. throw new IllegalStateException( "Java VM does not support a standard character set.", e); } }
/** * Helper called by generated code to construct default values for bytes * fields. * <p> * This is a lot like {@link #stringDefaultValue}, but for bytes fields. * In this case we only need the second of the two hacks -- allowing us to * embed raw bytes as a string literal with ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { try { return ByteString.copyFrom(bytes.getBytes("ISO-8859-1")); } catch (UnsupportedEncodingException e) { // This should never happen since all JVMs are required to implement // ISO-8859-1. throw new IllegalStateException( "Java VM does not support a standard character set.", e); } }
/** * Creates a byte string. Its size is the current size of this output * stream and its output has been copied to it. * * @return the current contents of this output stream, as a byte string. */ public synchronized ByteString toByteString() { flushLastBuffer(); return ByteString.copyFrom(flushedBuffers); }
/** * Creates a byte string. Its size is the current size of this output * stream and its output has been copied to it. * * @return the current contents of this output stream, as a byte string. */ public synchronized ByteString toByteString() { flushLastBuffer(); return ByteString.copyFrom(flushedBuffers); }
/** * Creates a byte string. Its size is the current size of this output * stream and its output has been copied to it. * * @return the current contents of this output stream, as a byte string. */ public synchronized ByteString toByteString() { flushLastBuffer(); return ByteString.copyFrom(flushedBuffers); }
/** Read a {@code bytes} field value from the stream. */ public ByteString readBytes() throws IOException { final int size = readRawVarint32(); if (size == 0) { return ByteString.EMPTY; } else if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final ByteString result = ByteString.copyFrom(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. return ByteString.copyFrom(readRawBytes(size)); } }
/** Read a {@code bytes} field value from the stream. */ public ByteString readBytes() throws IOException { final int size = readRawVarint32(); if (size == 0) { return ByteString.EMPTY; } else if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final ByteString result = ByteString.copyFrom(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. return ByteString.copyFrom(readRawBytes(size)); } }
public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) throws IOException { Collection<ByteString> results = new ArrayList<ByteString>(); // copy the inbound bytes into a list of chunks; the chunk size // grows exponentially to support both short and long streams. int chunkSize = minChunkSize; while (true) { ByteString chunk = readChunk(streamToDrain, chunkSize); if (chunk == null) { break; } results.add(chunk); chunkSize = Math.min(chunkSize * 2, maxChunkSize); } return ByteString.copyFrom(results); }
public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) throws IOException { Collection<ByteString> results = new ArrayList<ByteString>(); // copy the inbound bytes into a list of chunks; the chunk size // grows exponentially to support both short and long streams. int chunkSize = minChunkSize; while (true) { ByteString chunk = readChunk(streamToDrain, chunkSize); if (chunk == null) { break; } results.add(chunk); chunkSize = Math.min(chunkSize * 2, maxChunkSize); } return ByteString.copyFrom(results); }
public static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) throws IOException { Collection<ByteString> results = new ArrayList<ByteString>(); // copy the inbound bytes into a list of chunks; the chunk size // grows exponentially to support both short and long streams. int chunkSize = minChunkSize; while (true) { ByteString chunk = readChunk(streamToDrain, chunkSize); if (chunk == null) { break; } results.add(chunk); chunkSize = Math.min(chunkSize * 2, maxChunkSize); } return ByteString.copyFrom(results); }
/** Read a {@code bytes} field value from the stream. */ public ByteString readBytes() throws IOException { final int size = readRawVarint32(); if (size == 0) { return ByteString.EMPTY; } else if (size <= (bufferSize - bufferPos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. final ByteString result = ByteString.copyFrom(buffer, bufferPos, size); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. return ByteString.copyFrom(readRawBytes(size)); } }
/** * If the next token is a string, consume it, unescape it as a * {@link ByteString}, and return it. Otherwise, throw a * {@link ParseException}. */ public ByteString consumeByteString() throws ParseException { List<ByteString> list = new ArrayList<ByteString>(); consumeByteString(list); while (currentToken.startsWith("'") || currentToken.startsWith("\"")) { consumeByteString(list); } return ByteString.copyFrom(list); }
/** * If the next token is a string, consume it, unescape it as a * {@link ByteString}, and return it. Otherwise, throw a * {@link ParseException}. */ public ByteString consumeByteString() throws ParseException { List<ByteString> list = new ArrayList<ByteString>(); consumeByteString(list); while (currentToken.startsWith("'") || currentToken.startsWith("\"")) { consumeByteString(list); } return ByteString.copyFrom(list); }
/** * If the next token is a string, consume it, unescape it as a * {@link ByteString}, and return it. Otherwise, throw a * {@link ParseException}. */ public ByteString consumeByteString() throws ParseException { List<ByteString> list = new ArrayList<ByteString>(); consumeByteString(list); while (currentToken.startsWith("'") || currentToken.startsWith("\"")) { consumeByteString(list); } return ByteString.copyFrom(list); }