This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
feat: add SsFormat encoding library #4292
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
72f2d82
feat: add SsFormat encoding library
rahul2393 ae5ff12
remove unsigned methods
rahul2393 70348fc
Merge branch 'main' into feat/ssformat-encoding
rahul2393 c553e90
fix javadoc
rahul2393 d994efd
refactored appendInt method names + added tests for target range
rahul2393 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GrowableByteArrayOutputStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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.spanner.spi.v1; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * A simple, unsynchronized byte array output stream optimized for key encoding. | ||
| * | ||
| * <p>Unlike {@link java.io.ByteArrayOutputStream}, this class is not thread-safe and does not incur | ||
| * synchronization overhead. This provides better performance for single-threaded key encoding | ||
| * operations where synchronization is not required. | ||
| */ | ||
| @InternalApi | ||
| public final class GrowableByteArrayOutputStream { | ||
|
|
||
| private byte[] buf; | ||
| private int count; | ||
|
|
||
| /** Creates a new output stream with a default initial capacity of 32 bytes. */ | ||
| public GrowableByteArrayOutputStream() { | ||
| this(32); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new output stream with the specified initial capacity. | ||
| * | ||
| * @param initialCapacity the initial buffer size | ||
| * @throws IllegalArgumentException if initialCapacity is negative | ||
| */ | ||
| public GrowableByteArrayOutputStream(int initialCapacity) { | ||
| if (initialCapacity < 0) { | ||
| throw new IllegalArgumentException("Negative initial capacity: " + initialCapacity); | ||
| } | ||
| this.buf = new byte[initialCapacity]; | ||
| } | ||
|
|
||
| private void ensureCapacity(int minCapacity) { | ||
| if (minCapacity > buf.length) { | ||
| int newCapacity = Math.max(buf.length << 1, minCapacity); | ||
| buf = Arrays.copyOf(buf, newCapacity); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes the specified byte to this output stream. | ||
| * | ||
| * @param b the byte to write (only the low 8 bits are used) | ||
| */ | ||
| public void write(int b) { | ||
| ensureCapacity(count + 1); | ||
| buf[count++] = (byte) b; | ||
| } | ||
|
|
||
| /** | ||
| * Writes a portion of a byte array to this output stream. | ||
| * | ||
| * @param b the source byte array | ||
| * @param off the start offset in the array | ||
| * @param len the number of bytes to write | ||
| */ | ||
| public void write(byte[] b, int off, int len) { | ||
| ensureCapacity(count + len); | ||
| System.arraycopy(b, off, buf, count, len); | ||
| count += len; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a copy of the buffer contents as a new byte array. | ||
| * | ||
| * @return a new byte array containing the written bytes | ||
| */ | ||
| public byte[] toByteArray() { | ||
| return Arrays.copyOf(buf, count); | ||
| } | ||
|
|
||
| /** Resets the buffer so that it can be reused. The underlying buffer is retained. */ | ||
| public void reset() { | ||
| count = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current number of bytes written to this stream. | ||
| * | ||
| * @return the number of valid bytes in the buffer | ||
| */ | ||
| public int size() { | ||
| return count; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.