/** * Returns {@code true} if the size is {@code 0}, {@code false} otherwise. * * @return true if this is zero bytes long */ public boolean isEmpty() { return size() == 0; }
/** * Tests if this bytestring starts with the specified prefix. * Similar to {@link String#startsWith(String)} * * @param prefix the prefix. * @return <code>true</code> if the byte sequence represented by the * argument is a prefix of the byte sequence represented by * this string; <code>false</code> otherwise. */ public boolean startsWith(ByteString prefix) { return size() >= prefix.size() && substring(0, prefix.size()).equals(prefix); }
@Override public String toString() { return String.format("<ByteString@%s size=%d>", Integer.toHexString(System.identityHashCode(this)), size()); } }
@Override public final String toString() { return String.format("<ByteString@%s size=%d>", Integer.toHexString(System.identityHashCode(this)), size()); }
/** * Due to the optional field can be duplicated at the end of serialized * bytes, which will make the serialized size changed after LazyField * parsed. Be careful when using this method. */ public int getSerializedSize() { if (isDirty) { return value.getSerializedSize(); } return bytes.size(); }
/** * Return the substring from {@code beginIndex}, inclusive, to the end of the * string. * * @param beginIndex start at this index * @return substring sharing underlying data * @throws IndexOutOfBoundsException if {@code beginIndex < 0} or * {@code beginIndex > size()}. */ public ByteString substring(int beginIndex) { return substring(beginIndex, size()); }
/** * Return the substring from {@code beginIndex}, inclusive, to the end of the * string. * * @param beginIndex start at this index * @return substring sharing underlying data * @throws IndexOutOfBoundsException if {@code beginIndex < 0} or * {@code beginIndex > size()}. */ public final ByteString substring(int beginIndex) { return substring(beginIndex, size()); }
/** * Compute the number of bytes that would be needed to encode a * {@code bytes} field. */ public static int computeBytesSizeNoTag(final ByteString value) { return computeLengthDelimitedFieldSize(value.size()); }
/** * Copies bytes into a buffer at the given offset. * * @param target buffer to copy into * @param offset in the target buffer * @throws IndexOutOfBoundsException if the offset is negative or too large */ public void copyTo(byte[] target, int offset) { copyTo(target, 0, offset, size()); }
/** * Copies bytes to a {@code byte[]}. * * @return copied bytes */ public byte[] toByteArray() { int size = size(); byte[] result = new byte[size]; copyToInternal(result, 0, 0, size); return result; }
/** Write a byte string. */ public void writeRawBytes(final ByteString value) throws IOException { writeRawBytes(value, 0, value.size()); }
/** * Copies bytes into a buffer at the given offset. * * @param target buffer to copy into * @param offset in the target buffer * @throws IndexOutOfBoundsException if the offset is negative or too large */ public void copyTo(byte[] target, int offset) { copyTo(target, 0, offset, size()); }
@Override public boolean isValidUtf8() { int leftPartial = left.partialIsValidUtf8(Utf8.COMPLETE, 0, leftLength); int state = right.partialIsValidUtf8(leftPartial, 0, right.size()); return state == Utf8.COMPLETE; }
@Override public void writeBytesNoTag(final ByteString value) throws IOException { writeUInt32NoTag(value.size()); value.writeTo(this); }
@Override public void writeBytesNoTag(final ByteString value) throws IOException { writeUInt32NoTag(value.size()); value.writeTo(this); }
@Override public final void writeBytesNoTag(final ByteString value) throws IOException { writeUInt32NoTag(value.size()); value.writeTo(this); }
/** Write a {@code bytes} field to the stream. */ public void writeBytesNoTag(final ByteString value) throws IOException { writeRawVarint32(value.size()); writeRawBytes(value); }
@Override public DataBuffer readChunk() throws IOException { Preconditions.checkState(!mClient.isShutdown(), "Data reader is closed while reading data chunks."); ByteString buf; ReadResponse response = mStream.receive(mDataTimeoutMs); if (response == null) { return null; } Preconditions.checkState(response.hasChunk(), "response should always contain chunk"); buf = response.getChunk().getData(); mPosToRead += buf.size(); Preconditions.checkState(mPosToRead - mReadRequest.getOffset() <= mReadRequest.getLength()); return new NioDataBuffer(buf.asReadOnlyByteBuffer(), buf.size()); }
private void writeData(ByteString buf) { try { int readableBytes = buf.size(); mContext.setPos(mContext.getPos() + readableBytes); writeBuf(mContext, mResponseObserver, buf, mContext.getPos()); incrementMetrics(readableBytes); } catch (Exception e) { LOG.error("Failed to write data for request {}", mContext.getRequest(), e); Throwables.throwIfUnchecked(e); abort(new Error(AlluxioStatusException.fromCheckedException(e), true)); } }