Skip to content

Commit 05a2b25

Browse files
nathanmittlerejona86
authored andcommitted
New Buffer type for transport API.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=70056282
1 parent f4694f5 commit 05a2b25

File tree

10 files changed

+886
-0
lines changed

10 files changed

+886
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.google.net.stubby.newtransport;
2+
3+
/**
4+
* Abstract base class for {@link Buffer} implementations.
5+
*/
6+
public abstract class AbstractBuffer implements Buffer {
7+
8+
@Override
9+
public final int readUnsignedMedium() {
10+
checkReadable(3);
11+
int b1 = readUnsignedByte();
12+
int b2 = readUnsignedByte();
13+
int b3 = readUnsignedByte();
14+
return b1 << 16 | b2 << 8 | b3;
15+
}
16+
17+
18+
@Override
19+
public final int readUnsignedShort() {
20+
checkReadable(2);
21+
int b1 = readUnsignedByte();
22+
int b2 = readUnsignedByte();
23+
return b1 << 8 | b2;
24+
}
25+
26+
@Override
27+
public final int readInt() {
28+
checkReadable(4);
29+
int b1 = readUnsignedByte();
30+
int b2 = readUnsignedByte();
31+
int b3 = readUnsignedByte();
32+
int b4 = readUnsignedByte();
33+
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
34+
}
35+
36+
@Override
37+
public boolean hasArray() {
38+
return false;
39+
}
40+
41+
@Override
42+
public byte[] array() {
43+
throw new UnsupportedOperationException();
44+
}
45+
46+
@Override
47+
public int arrayOffset() {
48+
throw new UnsupportedOperationException();
49+
}
50+
51+
@Override
52+
public void close() {}
53+
54+
protected final void checkReadable(int length) {
55+
if (readableBytes() < length) {
56+
throw new IndexOutOfBoundsException();
57+
}
58+
}
59+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.google.net.stubby.newtransport;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.nio.ByteBuffer;
7+
8+
/**
9+
* Interface for an abstract byte buffer. Buffers are intended to be a read-only, except for the
10+
* read position which is incremented after each read call.
11+
*
12+
* <p>Buffers may optionally expose a backing array for optimization purposes, similar to what is
13+
* done in {@link ByteBuffer}. It is not expected that callers will attempt to modify the backing
14+
* array.
15+
*/
16+
public interface Buffer extends Closeable {
17+
18+
/**
19+
* Gets the current number of readable bytes remaining in this buffer.
20+
*/
21+
int readableBytes();
22+
23+
/**
24+
* Reads the next unsigned byte from this buffer and increments the read position by 1. If the
25+
* required bytes are not readable, throws {@link IndexOutOfBoundsException}.
26+
*/
27+
int readUnsignedByte();
28+
29+
/**
30+
* Reads a 3-byte unsigned integer from this buffer using big-endian byte ordering. Increments the
31+
* read position by 3. If the required bytes are not readable, throws
32+
* {@link IndexOutOfBoundsException}.
33+
*/
34+
int readUnsignedMedium();
35+
36+
/**
37+
* Reads a 2-byte unsigned integer from this buffer using big-endian byte ordering. Increments the
38+
* read position by 2. If the required bytes are not readable, throws
39+
* {@link IndexOutOfBoundsException}.
40+
*/
41+
int readUnsignedShort();
42+
43+
/**
44+
* Reads a 4-byte signed integer from this buffer using big-endian byte ordering. Increments the
45+
* read position by 4. If the required bytes are not readable, throws
46+
* {@link IndexOutOfBoundsException}.
47+
*/
48+
int readInt();
49+
50+
/**
51+
* Increments the read position by the given length. If the skipped bytes are not readable, throws
52+
* {@link IndexOutOfBoundsException}.
53+
*/
54+
void skipBytes(int length);
55+
56+
/**
57+
* Reads {@code length} bytes from this buffer and writes them to the destination array.
58+
* Increments the read position by {@code length}. If the required bytes are not readable or
59+
* {@code dest} array is to small, throws {@link IndexOutOfBoundsException}.
60+
*
61+
* @param dest the destination array to receive the bytes.
62+
* @param destOffset the starting offset in the destination array.
63+
* @param length the number of bytes to be copied.
64+
*/
65+
void readBytes(byte[] dest, int destOffset, int length);
66+
67+
/**
68+
* Reads from this buffer until the destination's position reaches its limit, and increases the
69+
* read position by the number of the transferred bytes. If the required bytes are not readable,
70+
* throws {@link IndexOutOfBoundsException}.
71+
*
72+
* @param dest the destination buffer to receive the bytes.
73+
*/
74+
void readBytes(ByteBuffer dest);
75+
76+
/**
77+
* Reads {@code length} bytes from this buffer and writes them to the destination stream.
78+
* Increments the read position by {@code length}. If the required bytes are not readable, throws
79+
* {@link IndexOutOfBoundsException}.
80+
*
81+
* @param dest the destination stream to receive the bytes.
82+
* @param length the number of bytes to be copied.
83+
* @throws IOException thrown if any error was encountered while writing to the stream.
84+
*/
85+
void readBytes(OutputStream dest, int length) throws IOException;
86+
87+
/**
88+
* Indicates whether or not this buffer exposes a backing array.
89+
*/
90+
boolean hasArray();
91+
92+
/**
93+
* Gets the backing array for this buffer. This is an optional method, so callers should first
94+
* check {@link #hasArray}. Buffers not supporting this method will throw
95+
* {@link UnsupportedOperationException}.
96+
*/
97+
byte[] array();
98+
99+
/**
100+
* Gets the offset in the backing array of the current read position. This is an optional method,
101+
* so callers should first check {@link #hasArray}. Buffers not supporting this method will throw
102+
* {@link UnsupportedOperationException}.
103+
*/
104+
int arrayOffset();
105+
106+
/**
107+
* Closes this buffer and releases any resources.
108+
*/
109+
@Override
110+
void close();
111+
}

0 commit comments

Comments
 (0)