Skip to content

Commit

Permalink
Fixes #1193: add BufferRecycler.Gettable, impls for BAB, SSW (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 19, 2024
1 parent ee93b3a commit 2b6e7a9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
* if so, instance of this class can be given as the writer to
* <code>JsonGenerator</code>.
*/
public final class SegmentedStringWriter extends Writer {
public final class SegmentedStringWriter
extends Writer
implements BufferRecycler.Gettable
{
final private TextBuffer _buffer;

public SegmentedStringWriter(BufferRecycler br) {
Expand All @@ -22,9 +25,20 @@ public SegmentedStringWriter(BufferRecycler br) {
}

/*
/**********************************************************
/**********************************************************************
/* BufferRecycler.Gettable implementation
/**********************************************************************
*/

@Override
public BufferRecycler bufferRecycler() {
return _buffer.bufferRecycler();
}

/*
/**********************************************************************
/* java.io.Writer implementation
/**********************************************************
/**********************************************************************
*/

@Override
Expand Down Expand Up @@ -59,6 +73,7 @@ public void write(char[] cbuf) throws IOException {
}

@Override

public void write(char[] cbuf, int off, int len) throws IOException {
_buffer.append(cbuf, off, len);
}
Expand All @@ -79,9 +94,9 @@ public void write(String str, int off, int len) throws IOException {
}

/*
/**********************************************************
/**********************************************************************
/* Extended API
/**********************************************************
/**********************************************************************
*/

/**
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/BufferRecycler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
public class BufferRecycler
implements RecyclerPool.WithPool<BufferRecycler>
{
/**
* Tag-on interface to allow various other types to expose {@link BufferRecycler}
* they are constructed with.
*
* @since 2.17
*/
public interface Gettable {
/**
* @return Buffer recycler instance object is configured with, if any;
* whether this can be {@code null} depends on type of object
*/
public BufferRecycler bufferRecycler();
}

/**
* Buffer used for reading byte-based input.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* theoretically this builder can aggregate more content it will not be usable
* as things are. Behavior may be improved if we solve the access problem.
*/
public final class ByteArrayBuilder extends OutputStream
public final class ByteArrayBuilder
extends OutputStream
implements BufferRecycler.Gettable
{
public final static byte[] NO_BYTES = new byte[0];

Expand Down Expand Up @@ -180,6 +182,17 @@ public byte[] toByteArray()
return result;
}

/*
/**********************************************************
/* BufferRecycler.Gettable implementation
/**********************************************************
*/

@Override
public BufferRecycler bufferRecycler() {
return _bufferRecycler;
}

/*
/**********************************************************
/* Non-stream API (similar to TextBuffer)
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ private void clearSegments()
/**********************************************************
*/

/**
* @since 2.17
*/
public BufferRecycler bufferRecycler() {
return _allocator;
}

/**
* @return Number of characters currently stored in this buffer
*/
Expand Down

0 comments on commit 2b6e7a9

Please sign in to comment.