Skip to content

Commit ae0a59b

Browse files
committed
Make bytes available
1 parent 0f17661 commit ae0a59b

File tree

10 files changed

+108
-31
lines changed

10 files changed

+108
-31
lines changed

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/DataSizeToBytesThriftCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public DataSizeToBytesThriftCodec(ThriftCatalog thriftCatalog)
4242
}
4343

4444
@CodecThriftType
45-
public static ThriftType getThriftType()
45+
public static ThriftType getThriftType(ThriftCatalog catalog)
4646
{
4747
return THRIFT_TYPE;
4848
}

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/DurationToMillisThriftCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public DurationToMillisThriftCodec(ThriftCatalog thriftCatalog)
4242
}
4343

4444
@CodecThriftType
45-
public static ThriftType getThriftType()
45+
public static ThriftType getThriftType(ThriftCatalog catalog)
4646
{
4747
return THRIFT_TYPE;
4848
}

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/JodaDateTimeToEpochMillisThriftCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public JodaDateTimeToEpochMillisThriftCodec(ThriftCatalog thriftCatalog)
4141
}
4242

4343
@CodecThriftType
44-
public static ThriftType getThriftType()
44+
public static ThriftType getThriftType(ThriftCatalog catalog)
4545
{
4646
return THRIFT_TYPE;
4747
}

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/LocaleToLanguageTagCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public LocaleToLanguageTagCodec(ThriftCatalog thriftCatalog)
4242
}
4343

4444
@CodecThriftType
45-
public static ThriftType getThriftType()
45+
public static ThriftType getThriftType(ThriftCatalog catalog)
4646
{
4747
return THRIFT_TYPE;
4848
}

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalDoubleThriftCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.facebook.drift.codec.CodecThriftType;
1919
import com.facebook.drift.codec.ThriftCodec;
20+
import com.facebook.drift.codec.metadata.ThriftCatalog;
2021
import com.facebook.drift.codec.metadata.ThriftType;
2122
import com.facebook.drift.protocol.TProtocolReader;
2223
import com.facebook.drift.protocol.TProtocolWriter;
@@ -34,7 +35,7 @@ public class OptionalDoubleThriftCodec
3435
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.DOUBLE, OptionalDouble.class, OptionalDouble.empty());
3536

3637
@CodecThriftType
37-
public static ThriftType getThriftType()
38+
public static ThriftType getThriftType(ThriftCatalog catalog)
3839
{
3940
return THRIFT_TYPE;
4041
}

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalIntThriftCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.facebook.drift.codec.CodecThriftType;
1919
import com.facebook.drift.codec.ThriftCodec;
20+
import com.facebook.drift.codec.metadata.ThriftCatalog;
2021
import com.facebook.drift.codec.metadata.ThriftType;
2122
import com.facebook.drift.protocol.TProtocolReader;
2223
import com.facebook.drift.protocol.TProtocolWriter;
@@ -40,7 +41,7 @@ public ThriftType getType()
4041
}
4142

4243
@CodecThriftType
43-
public static ThriftType getThriftType()
44+
public static ThriftType getThriftType(ThriftCatalog catalog)
4445
{
4546
return THRIFT_TYPE;
4647
}

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalLongThriftCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.facebook.drift.codec.CodecThriftType;
1919
import com.facebook.drift.codec.ThriftCodec;
20+
import com.facebook.drift.codec.metadata.ThriftCatalog;
2021
import com.facebook.drift.codec.metadata.ThriftType;
2122
import com.facebook.drift.protocol.TProtocolReader;
2223
import com.facebook.drift.protocol.TProtocolWriter;
@@ -34,7 +35,7 @@ public class OptionalLongThriftCodec
3435
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.I64, OptionalLong.class, OptionalLong.empty());
3536

3637
@CodecThriftType
37-
public static ThriftType getThriftType()
38+
public static ThriftType getThriftType(ThriftCatalog catalog)
3839
{
3940
return THRIFT_TYPE;
4041
}

drift-idl-generator/src/main/java/com/facebook/drift/idl/generator/ThriftIdlGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@ private void loadCustomCodec(String customCodecClassName)
162162
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must be static: " + customCodecClassName + "#" + method.getName());
163163
}
164164

165-
if (method.getParameterCount() != 0) {
166-
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must have no parameters: " + customCodecClassName + "#" + method.getName());
165+
if (method.getParameterCount() != 1 || method.getParameterTypes()[0] != ThriftCatalog.class) {
166+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must have only 1 parameter of ThriftCatalog type: " + customCodecClassName + "#" + method.getName());
167167
}
168168

169169
if (!ThriftType.class.isAssignableFrom(method.getReturnType())) {
170170
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must return ThriftType: " + customCodecClassName + "#" + method.getName());
171171
}
172172

173173
try {
174-
thriftType = (ThriftType) method.invoke(null);
174+
thriftType = (ThriftType) method.invoke(null, this.catalog);
175175
if (thriftType == null) {
176176
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType returns null: " + customCodecClassName + "#" + method.getName());
177177
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2017 Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.facebook.drift.protocol;
17+
18+
import java.io.ByteArrayOutputStream;
19+
import java.nio.charset.Charset;
20+
21+
public class TByteArrayOutputStream
22+
extends ByteArrayOutputStream
23+
{
24+
public TByteArrayOutputStream(int size)
25+
{
26+
super(size);
27+
}
28+
29+
public TByteArrayOutputStream()
30+
{
31+
super();
32+
}
33+
34+
protected byte[] get()
35+
{
36+
return buf;
37+
}
38+
39+
public int len()
40+
{
41+
return count;
42+
}
43+
44+
public String toString(Charset charset)
45+
{
46+
return new String(buf, 0, count, charset);
47+
}
48+
}

drift-protocol/src/main/java/com/facebook/drift/protocol/TMemoryBuffer.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,70 @@
1515
*/
1616
package com.facebook.drift.protocol;
1717

18-
import static java.lang.Math.max;
18+
import java.io.UnsupportedEncodingException;
1919

2020
public class TMemoryBuffer
2121
implements TTransport
2222
{
23-
private byte[] data;
24-
private int head;
25-
private int tail;
23+
// The contents of the buffer
24+
private TByteArrayOutputStream data;
2625

27-
public TMemoryBuffer(int initialSize)
26+
// Position to read next byte from
27+
private int position;
28+
29+
public TMemoryBuffer(int size)
2830
{
29-
data = new byte[max(initialSize, 16)];
31+
reset(size);
3032
}
3133

3234
@Override
3335
public void read(byte[] buf, int off, int len)
3436
throws TTransportException
3537
{
36-
if (head - tail < len) {
37-
throw new TTransportException("Too few bytes in buffer");
38+
byte[] src = data.get();
39+
int amtToRead = (len > data.len() - position ? data.len() - position : len);
40+
if (amtToRead > 0) {
41+
System.arraycopy(src, position, buf, off, amtToRead);
42+
position += amtToRead;
3843
}
39-
System.arraycopy(data, tail, buf, off, len);
40-
tail += len;
4144
}
4245

4346
@Override
4447
public void write(byte[] buf, int off, int len)
4548
{
46-
int available = data.length - head;
47-
if (available < len) {
48-
int need = len - available - tail;
49-
byte[] temp = new byte[max(data.length * 2, need)];
50-
System.arraycopy(data, tail, temp, 0, head - tail);
51-
data = temp;
52-
head -= tail;
53-
tail = 0;
54-
}
55-
System.arraycopy(buf, off, data, head, len);
56-
head += len;
49+
data.write(buf, off, len);
50+
}
51+
52+
/**
53+
* Output the contents of the memory buffer as a String, using the supplied encoding
54+
*
55+
* @param enc the encoding to use
56+
* @return the contents of the memory buffer as a String
57+
*/
58+
public String toString(String enc)
59+
throws UnsupportedEncodingException
60+
{
61+
return data.toString(enc);
62+
}
63+
64+
public int length()
65+
{
66+
return data.len();
67+
}
68+
69+
public void reset(int size)
70+
{
71+
data = new TByteArrayOutputStream(size);
72+
position = 0;
73+
}
74+
75+
public byte[] getBytes()
76+
{
77+
return data.get();
78+
}
79+
80+
public int getArrayPos()
81+
{
82+
return position;
5783
}
5884
}

0 commit comments

Comments
 (0)