Skip to content

Commit

Permalink
util: Make ProxyByteReader and ProxyByteWriter Public
Browse files Browse the repository at this point in the history
Problem

`ProxyByteReader` is incredibly useful for reading specialized information out
of a `Buf` while still benefitting from all the already existing
goodness that comes for free from the `ByteReader` implementation.
However, it is not currently accessible to code outside of the
`com.twitter` namespace.

Solution

Make `ProxyByteReader` and `ProxyByteWriter` both public, and modify
`ProxyByteReader`s structure so that it aligns with `ProxyByteWriter`.

Differential Revision: https://phabricator.twitter.biz/D622705
  • Loading branch information
ryanoneill authored and jenkins committed Feb 24, 2021
1 parent 955754d commit 35abeca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 71 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -8,9 +8,15 @@ Unreleased
----------

New Features
~~~~~~~~~~~~

* util-core: `c.t.io` now supports creating and deconstructing unsigned 128-bit buffers
in Buf. ``PHAB_ID=D606905``

* util-core: `c.t.io.ProxyByteReader` and `c.t.io.ProxyByteWriter` are now public. They are
useful for wrapping an existing `ByteReader` or `ByteWriter` and extending its functionality
without modifying the underlying instance. ``PHAB_ID=D622705``

21.2.0
------

Expand Down
68 changes: 0 additions & 68 deletions util-core/src/main/scala/com/twitter/io/ByteReader.scala
Expand Up @@ -203,74 +203,6 @@ trait ByteReader extends AutoCloseable {
def readAll(): Buf
}

private[twitter] trait ProxyByteReader extends ByteReader {
protected def reader: ByteReader

def remaining: Int = reader.remaining

def remainingUntil(byte: Byte): Int = reader.remainingUntil(byte)

def readByte(): Byte = reader.readByte()

def readUnsignedByte(): Short = reader.readUnsignedByte()

def readShortBE(): Short = reader.readShortBE()

def readShortLE(): Short = reader.readShortLE()

def readUnsignedShortBE(): Int = reader.readUnsignedShortBE()

def readUnsignedShortLE(): Int = reader.readUnsignedShortLE()

def readMediumBE(): Int = reader.readMediumBE()

def readMediumLE(): Int = reader.readMediumLE()

def readUnsignedMediumBE(): Int = reader.readUnsignedMediumBE()

def readUnsignedMediumLE(): Int = reader.readUnsignedMediumLE()

def readIntBE(): Int = reader.readIntBE()

def readIntLE(): Int = reader.readIntLE()

def readUnsignedIntBE(): Long = reader.readUnsignedIntBE()

def readUnsignedIntLE(): Long = reader.readUnsignedIntLE()

def readLongBE(): Long = reader.readLongBE()

def readLongLE(): Long = reader.readLongLE()

def readUnsignedLongBE(): BigInt = reader.readUnsignedLongBE()

def readUnsignedLongLE(): BigInt = reader.readUnsignedLongLE()

def readFloatBE(): Float = reader.readFloatBE()

def readFloatLE(): Float = reader.readFloatLE()

def readDoubleBE(): Double = reader.readDoubleBE()

def readDoubleLE(): Double = reader.readDoubleLE()

def readBytes(n: Int): Buf = reader.readBytes(n)

def readString(bytes: Int, charset: Charset): String = reader.readString(bytes, charset)

def skip(n: Int): Unit = reader.skip(n)

def readAll(): Buf = reader.readAll()

def process(from: Int, until: Int, processor: Buf.Processor): Int =
reader.process(from, until, processor)

def process(processor: Buf.Processor): Int =
reader.process(processor)

def close(): Unit = reader.close()
}

object ByteReader {

/**
Expand Down
74 changes: 74 additions & 0 deletions util-core/src/main/scala/com/twitter/io/ProxyByteReader.scala
@@ -0,0 +1,74 @@
package com.twitter.io

import java.nio.charset.Charset

/**
* A proxy [[ByteReader]] that forwards all calls to another [[ByteReader]].
* This is useful if you want to wrap-but-modify an existing [[ByteReader]].
*/
abstract class ProxyByteReader(underlying: ByteReader) extends ByteReader {

def remaining: Int = underlying.remaining

def remainingUntil(byte: Byte): Int = underlying.remainingUntil(byte)

def readByte(): Byte = underlying.readByte()

def readUnsignedByte(): Short = underlying.readUnsignedByte()

def readShortBE(): Short = underlying.readShortBE()

def readShortLE(): Short = underlying.readShortLE()

def readUnsignedShortBE(): Int = underlying.readUnsignedShortBE()

def readUnsignedShortLE(): Int = underlying.readUnsignedShortLE()

def readMediumBE(): Int = underlying.readMediumBE()

def readMediumLE(): Int = underlying.readMediumLE()

def readUnsignedMediumBE(): Int = underlying.readUnsignedMediumBE()

def readUnsignedMediumLE(): Int = underlying.readUnsignedMediumLE()

def readIntBE(): Int = underlying.readIntBE()

def readIntLE(): Int = underlying.readIntLE()

def readUnsignedIntBE(): Long = underlying.readUnsignedIntBE()

def readUnsignedIntLE(): Long = underlying.readUnsignedIntLE()

def readLongBE(): Long = underlying.readLongBE()

def readLongLE(): Long = underlying.readLongLE()

def readUnsignedLongBE(): BigInt = underlying.readUnsignedLongBE()

def readUnsignedLongLE(): BigInt = underlying.readUnsignedLongLE()

def readFloatBE(): Float = underlying.readFloatBE()

def readFloatLE(): Float = underlying.readFloatLE()

def readDoubleBE(): Double = underlying.readDoubleBE()

def readDoubleLE(): Double = underlying.readDoubleLE()

def readBytes(n: Int): Buf = underlying.readBytes(n)

def readString(bytes: Int, charset: Charset): String = underlying.readString(bytes, charset)

def skip(n: Int): Unit = underlying.skip(n)

def readAll(): Buf = underlying.readAll()

def process(from: Int, until: Int, processor: Buf.Processor): Int =
underlying.process(from, until, processor)

def process(processor: Buf.Processor): Int =
underlying.process(processor)

def close(): Unit = underlying.close()
}
6 changes: 3 additions & 3 deletions util-core/src/main/scala/com/twitter/io/ProxyByteWriter.scala
Expand Up @@ -3,10 +3,10 @@ package com.twitter.io
import java.nio.charset.Charset

/**
* A simple proxy ByteWriter that forwards all calls to another ByteWriter.
* This is useful if you want to wrap-but-modify an existing ByteWriter.
* A proxy [[ByteWriter]] that forwards all calls to another [[ByteWriter]].
* This is useful if you want to wrap-but-modify an existing [[ByteWriter]].
*/
private[twitter] abstract class ProxyByteWriter(underlying: ByteWriter) extends AbstractByteWriter {
abstract class ProxyByteWriter(underlying: ByteWriter) extends AbstractByteWriter {

def writeString(string: CharSequence, charset: Charset): ProxyByteWriter.this.type = {
underlying.writeString(string, charset)
Expand Down

0 comments on commit 35abeca

Please sign in to comment.