use of java.nio.ByteBuffer in project stargate-core by tuplejump.
the class TableMapper method start.
public final Composite start(CellName cellName) {
CBuilder builder = clusteringCType.builder();
for (int i = 0; i < cellName.clusteringSize(); i++) {
ByteBuffer component = cellName.get(i);
builder.add(component);
}
return builder.build();
}
use of java.nio.ByteBuffer in project stargate-core by tuplejump.
the class IndexEventHandler method onEvent.
@Override
public void onEvent(IndexEntryEvent event, long sequence, boolean endOfBatch) throws Exception {
if ((sequence % numberOfConsumers) == ordinal) {
ByteBuffer rowkeyBuffer = event.getRowKey();
ColumnFamily columnFamily = event.getColumnFamily();
final RowIndexSupport rowIndexSupport = indexingService.support.get(columnFamily.metadata().cfName);
try {
rowIndexSupport.indexRow(rowkeyBuffer, columnFamily);
} catch (Exception e) {
logger.error("Error occurred while indexing row of [" + columnFamily.metadata().cfName + "]", e);
} finally {
event.setData(null, null);
long readGen = indexingService.reads.incrementAndGet();
if (logger.isDebugEnabled())
logger.debug("Read gen:" + readGen);
}
}
}
use of java.nio.ByteBuffer in project distributedlog by twitter.
the class WriterWorker method buildBufferList.
List<ByteBuffer> buildBufferList(int batchSize, long requestMillis, int messageSizeBytes) {
ArrayList<ByteBuffer> bufferList = new ArrayList<ByteBuffer>(batchSize);
for (int i = 0; i < batchSize; i++) {
ByteBuffer buf = buildBuffer(requestMillis, messageSizeBytes);
if (null == buf) {
return null;
}
bufferList.add(buf);
}
return bufferList;
}
use of java.nio.ByteBuffer in project bazel by bazelbuild.
the class ZipInTest method testEndOfCentralDirectory.
/**
* Test of endOfCentralDirectory method, of class ZipIn.
*/
@Test
public void testEndOfCentralDirectory() throws Exception {
String filename = "test.zip";
byte[] bytes;
ByteBuffer buffer;
String comment;
int commentLen;
int offset;
ZipIn zipIn;
EndOfCentralDirectory result;
String subcase;
// Find it, even if it's the only useful thing in the file.
subcase = " EOCD found it, ";
bytes = new byte[] { 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
result = zipIn.endOfCentralDirectory();
assertNotNull(subcase + "found", result);
subcase = " EOCD not there at all, ";
bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
try {
zipIn.endOfCentralDirectory();
fail(subcase + "expected IllegalStateException");
} catch (Exception ex) {
assertSame(subcase + "caught exception", IllegalStateException.class, ex.getClass());
}
// If we can't read it, it's not there
subcase = " EOCD too late to read, ";
bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
try {
zipIn.endOfCentralDirectory();
fail(subcase + "expected IndexOutOfBoundsException");
} catch (Exception ex) {
assertSame(subcase + "caught exception", IndexOutOfBoundsException.class, ex.getClass());
}
// Current implementation doesn't know to scan past a bad EOCD record.
// I'm not sure if it should.
subcase = " EOCD good hidden by bad, ";
bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
try {
zipIn.endOfCentralDirectory();
fail(subcase + "expected IndexOutOfBoundsException");
} catch (Exception ex) {
assertSame(subcase + "caught exception", IndexOutOfBoundsException.class, ex.getClass());
}
// Minimal format checking here, assuming the EndOfDirectoryTest class
// test for that.
subcase = " EOCD truncated comment, ";
bytes = new byte[100];
buffer = ByteBuffer.wrap(bytes);
comment = "optional file comment";
commentLen = comment.getBytes(UTF_8).length;
offset = bytes.length - ZipInputStream.ENDHDR - commentLen;
buffer.position(offset);
EndOfCentralDirectory.view(buffer, comment);
byte[] truncated = Arrays.copyOf(bytes, bytes.length - 5);
fileSystem.addFile(filename, truncated);
zipIn = newZipIn(filename);
try {
// not sure this is the exception we want!
zipIn.endOfCentralDirectory();
fail(subcase + "expected IllegalArgumentException");
} catch (Exception ex) {
assertSame(subcase + "caught exception", IllegalArgumentException.class, ex.getClass());
}
subcase = " EOCD no comment, ";
bytes = new byte[100];
buffer = ByteBuffer.wrap(bytes);
comment = null;
commentLen = 0;
offset = bytes.length - ZipInputStream.ENDHDR - commentLen;
buffer.position(offset);
EndOfCentralDirectory.view(buffer, comment);
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
result = zipIn.endOfCentralDirectory();
assertNotNull(subcase + "found", result);
assertEquals(subcase + "comment", "", result.getComment());
assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG));
subcase = " EOCD comment, ";
bytes = new byte[100];
buffer = ByteBuffer.wrap(bytes);
comment = "optional file comment";
commentLen = comment.getBytes(UTF_8).length;
offset = bytes.length - ZipInputStream.ENDHDR - commentLen;
buffer.position(offset);
EndOfCentralDirectory.view(buffer, comment);
assertEquals(subcase + "setup", comment, new String(bytes, bytes.length - commentLen, commentLen, UTF_8));
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
result = zipIn.endOfCentralDirectory();
assertNotNull(subcase + "found", result);
assertEquals(subcase + "comment", comment, result.getComment());
assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG));
subcase = " EOCD extra data, ";
bytes = new byte[100];
buffer = ByteBuffer.wrap(bytes);
comment = null;
commentLen = 0;
offset = bytes.length - ZipInputStream.ENDHDR - commentLen - 10;
buffer.position(offset);
EndOfCentralDirectory.view(buffer, comment);
fileSystem.addFile(filename, bytes);
zipIn = newZipIn(filename);
result = zipIn.endOfCentralDirectory();
assertNotNull(subcase + "found", result);
assertEquals(subcase + "comment", "", result.getComment());
assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG));
}
use of java.nio.ByteBuffer in project Japid by branaway.
the class StringUtilsTest method testSpeed.
@Test
public void testSpeed() throws IOException {
String src = "自由交往";
StringBuffer sb = new StringBuffer(10000);
for (int i = 0; i < 4000; i++) {
sb.append(src);
}
src = sb.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
long t1 = 0, t2 = 0;
ByteBuffer bb = StringUtils.encodeUTF8(src);
bb = StringUtils.encodeUTF8(src);
bb = StringUtils.encodeUTF8(src);
bb = StringUtils.encodeUTF8(src);
baos.write(src.getBytes("UTF-8"));
baos.write(src.getBytes("UTF-8"));
baos.write(src.getBytes("UTF-8"));
for (int i = 0; i < 200; i++) {
{
baos.reset();
long t = System.nanoTime();
bb = StringUtils.encodeUTF8(src);
byte[] array = bb.array();
int p = bb.position();
baos.write(array, 0, p);
t1 += System.nanoTime() - t;
}
{
baos.reset();
long t = System.nanoTime();
baos.write(src.getBytes("UTF-8"));
t2 += System.nanoTime() - t;
}
}
System.out.println("my encoder took: " + t1);
System.out.println("String.getBytes() took:: " + t2);
}
Aggregations