Skip to content

Commit 5c77869

Browse files
Modify libraries to use the MutableDirectBuffer/DirectBuffer interface
1 parent d4f9372 commit 5c77869

File tree

12 files changed

+85
-68
lines changed

12 files changed

+85
-68
lines changed

main/java/uk/co/real_logic/sbe/codec/java/CodecUtil.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@
1515
*/
1616
package uk.co.real_logic.sbe.codec.java;
1717

18+
import uk.co.real_logic.agrona.DirectBuffer;
19+
import uk.co.real_logic.agrona.MutableDirectBuffer;
20+
1821
import java.nio.ByteOrder;
1922

2023
public class CodecUtil
2124
{
2225
/**
23-
* Put a character to a {@link DirectBuffer} at the given index.
26+
* Put a character to a {@link MutableDirectBuffer} at the given index.
2427
*
2528
* @param buffer to which the value should be written.
2629
* @param index from which to begin writing.
2730
* @param value to be be written.
2831
*/
29-
public static void charPut(final DirectBuffer buffer, final int index, final byte value)
32+
public static void charPut(final MutableDirectBuffer buffer, final int index, final byte value)
3033
{
3134
buffer.putByte(index, value);
3235
}
3336

3437
/**
35-
* Put an array into a {@link DirectBuffer} at the given index.
38+
* Put an array into a {@link MutableDirectBuffer} at the given index.
3639
*
3740
* @param buffer to which the value should be written.
3841
* @param index from which to begin writing.
@@ -41,25 +44,25 @@ public static void charPut(final DirectBuffer buffer, final int index, final byt
4144
* @param length of the src buffer to copy.
4245
*/
4346
public static void charsPut(
44-
final DirectBuffer buffer, final int index, final byte[] src, final int offset, final int length)
47+
final MutableDirectBuffer buffer, final int index, final byte[] src, final int offset, final int length)
4548
{
4649
buffer.putBytes(index, src, offset, length);
4750
}
4851

4952
/**
50-
* Put a 8-bit integer to a {@link DirectBuffer} at the given index.
53+
* Put a 8-bit integer to a {@link MutableDirectBuffer} at the given index.
5154
*
5255
* @param buffer to which the value should be written.
5356
* @param index from which to begin writing.
5457
* @param value to be be written.
5558
*/
56-
public static void int8Put(final DirectBuffer buffer, final int index, final byte value)
59+
public static void int8Put(final MutableDirectBuffer buffer, final int index, final byte value)
5760
{
5861
buffer.putByte(index, value);
5962
}
6063

6164
/**
62-
* Put an array into a {@link DirectBuffer} at the given index.
65+
* Put an array into a {@link MutableDirectBuffer} at the given index.
6366
*
6467
* @param buffer to which the value should be written.
6568
* @param index from which to begin writing.
@@ -68,98 +71,98 @@ public static void int8Put(final DirectBuffer buffer, final int index, final byt
6871
* @param length of the src buffer to copy.
6972
*/
7073
public static void int8sPut(
71-
final DirectBuffer buffer, final int index, final byte[] src, final int offset, final int length)
74+
final MutableDirectBuffer buffer, final int index, final byte[] src, final int offset, final int length)
7275
{
7376
buffer.putBytes(index, src, offset, length);
7477
}
7578

7679
/**
77-
* Put a 16-bit integer to a {@link DirectBuffer} at the given index.
80+
* Put a 16-bit integer to a {@link MutableDirectBuffer} at the given index.
7881
*
7982
* @param buffer to which the value should be written.
8083
* @param index from which to begin writing.
8184
* @param value to be be written.
8285
* @param byteOrder for the buffer encoding
8386
*/
84-
public static void int16Put(final DirectBuffer buffer, final int index, final short value, final ByteOrder byteOrder)
87+
public static void int16Put(final MutableDirectBuffer buffer, final int index, final short value, final ByteOrder byteOrder)
8588
{
8689
buffer.putShort(index, value, byteOrder);
8790
}
8891

8992
/**
90-
* Put a 32-bit integer to a {@link DirectBuffer} at the given index.
93+
* Put a 32-bit integer to a {@link MutableDirectBuffer} at the given index.
9194
*
9295
* @param buffer to which the value should be written.
9396
* @param index from which to begin writing.
9497
* @param value to be be written.
9598
* @param byteOrder for the buffer encoding
9699
*/
97-
public static void int32Put(final DirectBuffer buffer, final int index, final int value, final ByteOrder byteOrder)
100+
public static void int32Put(final MutableDirectBuffer buffer, final int index, final int value, final ByteOrder byteOrder)
98101
{
99102
buffer.putInt(index, value, byteOrder);
100103
}
101104

102105
/**
103-
* Put a 64-bit integer to a {@link DirectBuffer} at the given index.
106+
* Put a 64-bit integer to a {@link MutableDirectBuffer} at the given index.
104107
*
105108
* @param buffer to which the value should be written.
106109
* @param index from which to begin writing.
107110
* @param value to be be written.
108111
* @param byteOrder for the buffer encoding
109112
*/
110-
public static void int64Put(final DirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
113+
public static void int64Put(final MutableDirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
111114
{
112115
buffer.putLong(index, value, byteOrder);
113116
}
114117

115118
/**
116-
* Put a 8-bit signed integer to a {@link DirectBuffer} at the given index.
119+
* Put a 8-bit signed integer to a {@link MutableDirectBuffer} at the given index.
117120
*
118121
* @param buffer to which the value should be written.
119122
* @param index from which to begin writing.
120123
* @param value to be be written.
121124
* @throws IllegalArgumentException if the number is negative
122125
*/
123-
public static void uint8Put(final DirectBuffer buffer, final int index, final short value)
126+
public static void uint8Put(final MutableDirectBuffer buffer, final int index, final short value)
124127
{
125128
buffer.putByte(index, (byte)value);
126129
}
127130

128131
/**
129-
* Put a 16-bit signed integer to a {@link DirectBuffer} at the given index.
132+
* Put a 16-bit signed integer to a {@link MutableDirectBuffer} at the given index.
130133
*
131134
* @param buffer to which the value should be written.
132135
* @param index from which to begin writing.
133136
* @param value to be be written.
134137
* @param byteOrder for the buffer encoding
135138
*/
136-
public static void uint16Put(final DirectBuffer buffer, final int index, final int value, final ByteOrder byteOrder)
139+
public static void uint16Put(final MutableDirectBuffer buffer, final int index, final int value, final ByteOrder byteOrder)
137140
{
138141
buffer.putShort(index, (short)value, byteOrder);
139142
}
140143

141144
/**
142-
* Put a 32-bit signed integer to a {@link DirectBuffer} at the given index.
145+
* Put a 32-bit signed integer to a {@link MutableDirectBuffer} at the given index.
143146
*
144147
* @param buffer to which the value should be written.
145148
* @param index from which to begin writing.
146149
* @param value to be be written.
147150
* @param byteOrder for the buffer encoding
148151
*/
149-
public static void uint32Put(final DirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
152+
public static void uint32Put(final MutableDirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
150153
{
151154
buffer.putInt(index, (int)value, byteOrder);
152155
}
153156

154157
/**
155-
* Put a 64-bit signed integer to a {@link DirectBuffer} at the given index.
158+
* Put a 64-bit signed integer to a {@link MutableDirectBuffer} at the given index.
156159
*
157160
* @param buffer to which the value should be written.
158161
* @param index from which to begin writing.
159162
* @param value to be be written.
160163
* @param byteOrder for the buffer encoding
161164
*/
162-
public static void uint64Put(final DirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
165+
public static void uint64Put(final MutableDirectBuffer buffer, final int index, final long value, final ByteOrder byteOrder)
163166
{
164167
buffer.putLong(index, value, byteOrder);
165168
}
@@ -172,7 +175,7 @@ public static void uint64Put(final DirectBuffer buffer, final int index, final l
172175
* @param value to be be written.
173176
* @param byteOrder for the buffer encoding
174177
*/
175-
public static void floatPut(final DirectBuffer buffer, final int index, final float value, final ByteOrder byteOrder)
178+
public static void floatPut(final MutableDirectBuffer buffer, final int index, final float value, final ByteOrder byteOrder)
176179
{
177180
buffer.putFloat(index, value, byteOrder);
178181
}
@@ -185,7 +188,7 @@ public static void floatPut(final DirectBuffer buffer, final int index, final fl
185188
* @param value to be be written.
186189
* @param byteOrder for the buffer encoding
187190
*/
188-
public static void doublePut(final DirectBuffer buffer, final int index, final double value, final ByteOrder byteOrder)
191+
public static void doublePut(final MutableDirectBuffer buffer, final int index, final double value, final ByteOrder byteOrder)
189192
{
190193
buffer.putDouble(index, value, byteOrder);
191194
}
@@ -379,7 +382,7 @@ public static boolean uint8GetChoice(final DirectBuffer buffer, final int index,
379382
* @param bitIndex bit index to set.
380383
* @param switchOn true sets bit to 1 and false sets it to 0.
381384
*/
382-
public static void uint8PutChoice(final DirectBuffer buffer, final int index, final int bitIndex, boolean switchOn)
385+
public static void uint8PutChoice(final MutableDirectBuffer buffer, final int index, final int bitIndex, boolean switchOn)
383386
{
384387
byte bits = buffer.getByte(index);
385388
bits = (byte)(switchOn ? bits | (1 << bitIndex) : bits & ~(1 << bitIndex));
@@ -411,7 +414,7 @@ public static boolean uint16GetChoice(
411414
* @param byteOrder for the buffer encoding
412415
*/
413416
public static void uint16PutChoice(
414-
final DirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
417+
final MutableDirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
415418
{
416419
short bits = buffer.getShort(index, byteOrder);
417420
bits = (short)(switchOn ? bits | (1 << bitIndex) : bits & ~(1 << bitIndex));
@@ -443,7 +446,7 @@ public static boolean uint32GetChoice(
443446
* @param byteOrder for the buffer encoding
444447
*/
445448
public static void uint32PutChoice(
446-
final DirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
449+
final MutableDirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
447450
{
448451
int bits = buffer.getInt(index, byteOrder);
449452
bits = switchOn ? bits | (1 << bitIndex) : bits & ~(1 << bitIndex);
@@ -475,7 +478,7 @@ public static boolean uint64GetChoice(
475478
* @param byteOrder for the buffer encoding
476479
*/
477480
public static void uint64PutChoice(
478-
final DirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
481+
final MutableDirectBuffer buffer, final int index, final int bitIndex, final boolean switchOn, final ByteOrder byteOrder)
479482
{
480483
long bits = buffer.getLong(index, byteOrder);
481484
bits = switchOn ? bits | (1L << bitIndex) : bits & ~(1L << bitIndex);

main/java/uk/co/real_logic/sbe/generation/TargetCodeGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package uk.co.real_logic.sbe.generation;
1717

18+
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;
1819
import uk.co.real_logic.sbe.SbeTool;
19-
import uk.co.real_logic.sbe.codec.java.DirectBuffer;
2020
import uk.co.real_logic.sbe.generation.cpp98.Cpp98Generator;
2121
import uk.co.real_logic.sbe.generation.cpp98.NamespaceOutputManager;
2222
import uk.co.real_logic.sbe.generation.csharp.CSharpGenerator;
@@ -50,7 +50,7 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
5050
throws IOException
5151
{
5252
return new JavaGenerator(ir,
53-
System.getProperty(SbeTool.JAVA_BUFFER, DirectBuffer.class.getName()),
53+
System.getProperty(SbeTool.JAVA_BUFFER, UnsafeBuffer.class.getName()),
5454
new PackageOutputManager(outputDir, ir.applicableNamespace()));
5555
}
5656
},

main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package uk.co.real_logic.sbe.generation.java;
1717

18+
import uk.co.real_logic.agrona.DirectBuffer;
19+
import uk.co.real_logic.agrona.MutableDirectBuffer;
1820
import uk.co.real_logic.sbe.PrimitiveType;
19-
import uk.co.real_logic.sbe.codec.java.DirectBuffer;
2021
import uk.co.real_logic.sbe.generation.CodeGenerator;
2122
import uk.co.real_logic.sbe.generation.OutputManager;
22-
import uk.co.real_logic.sbe.ir.*;
23+
import uk.co.real_logic.sbe.ir.Encoding;
24+
import uk.co.real_logic.sbe.ir.Ir;
25+
import uk.co.real_logic.sbe.ir.Signal;
26+
import uk.co.real_logic.sbe.ir.Token;
2327
import uk.co.real_logic.sbe.util.Verify;
2428

2529
import java.io.IOException;
@@ -59,7 +63,7 @@ private String validateBufferImplementation(final String fullyQualifiedBufferImp
5963
try
6064
{
6165
final Class<?> cls = Class.forName(fullyQualifiedBufferImplementation);
62-
if (!cls.isAssignableFrom(DirectBuffer.class))
66+
if (!cls.isAssignableFrom(MutableDirectBuffer.class))
6367
{
6468
throw new IllegalArgumentException(
6569
fullyQualifiedBufferImplementation + " doesn't implement " + DirectBuffer.class.getName());
@@ -1529,4 +1533,4 @@ private String generateLiteral(final PrimitiveType type, final String value)
15291533

15301534
return literal;
15311535
}
1532-
}
1536+
}

main/java/uk/co/real_logic/sbe/ir/IrDecoder.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
*/
1616
package uk.co.real_logic.sbe.ir;
1717

18+
import uk.co.real_logic.agrona.MutableDirectBuffer;
19+
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;
1820
import uk.co.real_logic.sbe.PrimitiveType;
19-
import uk.co.real_logic.sbe.codec.java.DirectBuffer;
2021
import uk.co.real_logic.sbe.ir.generated.FrameCodec;
21-
2222
import uk.co.real_logic.sbe.ir.generated.TokenCodec;
2323

24-
import java.io.*;
24+
import java.io.Closeable;
25+
import java.io.IOException;
26+
import java.io.RandomAccessFile;
27+
import java.io.UnsupportedEncodingException;
2528
import java.nio.ByteBuffer;
2629
import java.nio.MappedByteBuffer;
2730
import java.nio.channels.FileChannel;
@@ -35,7 +38,7 @@ public class IrDecoder implements Closeable
3538
private static final int CAPACITY = 4096;
3639

3740
private final FileChannel channel;
38-
private final DirectBuffer directBuffer;
41+
private final MutableDirectBuffer directBuffer;
3942
private final FrameCodec frameCodec = new FrameCodec();
4043
private final TokenCodec tokenCodec = new TokenCodec();
4144
private int offset;
@@ -47,15 +50,15 @@ public class IrDecoder implements Closeable
4750
private int irId;
4851
private int irVersion = 0;
4952
private final byte[] valArray = new byte[CAPACITY];
50-
private final DirectBuffer valBuffer = new DirectBuffer(valArray);
53+
private final MutableDirectBuffer valBuffer = new UnsafeBuffer(valArray);
5154
private final byte[] buffer = new byte[1024];
5255

5356
public IrDecoder(final String fileName)
5457
throws IOException
5558
{
5659
channel = new RandomAccessFile(fileName, "r").getChannel();
5760
final MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
58-
directBuffer = new DirectBuffer(buffer);
61+
directBuffer = new UnsafeBuffer(buffer);
5962
size = channel.size();
6063
offset = 0;
6164
}
@@ -64,7 +67,7 @@ public IrDecoder(final ByteBuffer buffer)
6467
{
6568
channel = null;
6669
size = buffer.limit();
67-
directBuffer = new DirectBuffer(buffer);
70+
directBuffer = new UnsafeBuffer(buffer);
6871
offset = 0;
6972
}
7073

main/java/uk/co/real_logic/sbe/ir/IrEncoder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616
package uk.co.real_logic.sbe.ir;
1717

18+
import uk.co.real_logic.agrona.MutableDirectBuffer;
19+
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;
1820
import uk.co.real_logic.sbe.PrimitiveType;
19-
import uk.co.real_logic.sbe.codec.java.DirectBuffer;
2021
import uk.co.real_logic.sbe.ir.generated.FrameCodec;
2122
import uk.co.real_logic.sbe.ir.generated.TokenCodec;
2223
import uk.co.real_logic.sbe.util.Verify;
@@ -35,12 +36,12 @@ public class IrEncoder implements Closeable
3536
private final FileChannel channel;
3637
private final ByteBuffer resultBuffer;
3738
private final ByteBuffer buffer;
38-
private final DirectBuffer directBuffer;
39+
private final MutableDirectBuffer directBuffer;
3940
private final Ir ir;
4041
private final FrameCodec frameCodec = new FrameCodec();
4142
private final TokenCodec tokenCodec = new TokenCodec();
4243
private final byte[] valArray = new byte[CAPACITY];
43-
private final DirectBuffer valBuffer = new DirectBuffer(valArray);
44+
private final UnsafeBuffer valBuffer = new UnsafeBuffer(valArray);
4445
private int totalSize = 0;
4546

4647
public IrEncoder(final String fileName, final Ir ir)
@@ -49,7 +50,7 @@ public IrEncoder(final String fileName, final Ir ir)
4950
channel = new FileOutputStream(fileName).getChannel();
5051
resultBuffer = null;
5152
buffer = ByteBuffer.allocateDirect(CAPACITY);
52-
directBuffer = new DirectBuffer(buffer);
53+
directBuffer = new UnsafeBuffer(buffer);
5354
this.ir = ir;
5455
}
5556

@@ -58,7 +59,7 @@ public IrEncoder(final ByteBuffer buffer, final Ir ir)
5859
channel = null;
5960
resultBuffer = buffer;
6061
this.buffer = ByteBuffer.allocateDirect(CAPACITY);
61-
directBuffer = new DirectBuffer(this.buffer);
62+
directBuffer = new UnsafeBuffer(this.buffer);
6263
this.ir = ir;
6364
}
6465

0 commit comments

Comments
 (0)