本文整理汇总了Java中java.nio.ByteBuffer.flip方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.flip方法的具体用法?Java ByteBuffer.flip怎么用?Java ByteBuffer.flip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.flip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tlsDecrypt
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static byte[] tlsDecrypt(SSLEngine tlsEngine,
ByteBuffer appDataBuf,
ByteBuffer netDataBuf,
byte[] netData){
try {
appDataBuf.clear();
netDataBuf.clear();
netDataBuf.put(netData);
netDataBuf.flip();
int consumed = tlsEngine.unwrap(netDataBuf, appDataBuf).bytesConsumed();
while (consumed < netData.length){
consumed += tlsEngine.unwrap(netDataBuf, appDataBuf).bytesConsumed();
}
appDataBuf.flip();
byte[] appData = new byte[appDataBuf.limit()];
appDataBuf.get(appData);
return appData;
} catch (SSLException e){
Log.e("TlsHelper", "tlsDecrypt");
Log.e("StackTrace", Log.getStackTraceString(e));
return null;
}
}
示例2: write
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Dispose of a ByteBuffer which has been acquired for writing by one of
* the write methods, writing its contents to the file.
*/
public int write (ByteBuffer bb) throws IOException {
synchronized (this) {
if (bb == buffer) {
buffer = null;
}
}
int position = size();
int byteCount = bb.position();
bb.flip();
FileChannel channel = writeChannel();
if (channel.isOpen()) { //If a thread was terminated while writing, it will be closed
Thread.interrupted(); // #186629: must clear interrupt flag or channel will be broken
channel.write (bb);
synchronized (this) {
bytesWritten += byteCount;
outstandingBufferCount--;
}
}
return position;
}
示例3: testDecodeEmptySequence
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void testDecodeEmptySequence()
{
Asn1Decoder decoder = new Asn1Decoder();
ByteBuffer bb = ByteBuffer.allocate( 2 );
bb.put( new byte[]
{ 0x30, 0x00 } ); // CertGenerateObject ::= SEQUENCE {
CertGenerationContainer container = new CertGenerationContainer();
bb.flip();
try
{
decoder.decode( bb, container );
// The PDU with an empty sequence is not allowed
fail();
}
catch ( DecoderException e )
{
assertTrue( true );
}
}
示例4: testEncodePagedSearchControl
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test encoding of a PagedSearchControl.
*/
@Test
public void testEncodePagedSearchControl() throws Exception
{
ByteBuffer bb = ByteBuffer.allocate( 0x0B );
bb.put( new byte[]
{
0x30, 0x09, // realSearchControlValue ::= SEQUENCE {
0x02,
0x01,
0x20, // size INTEGER,
0x04,
0x04,
't',
'e',
's',
't' // cookie OCTET STRING,
} );
bb.flip();
PagedResultsDecorator decorator = new PagedResultsDecorator( codec );
PagedResults pagedSearch = ( PagedResults ) decorator.decode( bb.array() );
assertEquals( 32, pagedSearch.getSize() );
assertTrue( Arrays.equals( Strings.getBytesUtf8( "test" ),
pagedSearch.getCookie() ) );
bb.flip();
PagedResultsDecorator ctrl = new PagedResultsDecorator( codec );
ctrl.setSize( 32 );
ctrl.setCookie( Strings.getBytesUtf8( "test" ) );
ByteBuffer buffer = ctrl.encode( ByteBuffer.allocate( ctrl.computeLength() ) );
String decoded = Strings.dumpBytes( buffer.array() );
String expected = Strings.dumpBytes( bb.array() );
assertEquals( expected, decoded );
}
示例5: testDecodeAbandonRequestBadMessageId
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test the decoding of a AbandonRequest with a bad Message Id
*/
@Test
public void testDecodeAbandonRequestBadMessageId()
{
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate( 0x0B );
stream.put( new byte[]
{ 0x30, 0x09, // LDAPMessage ::=SEQUENCE {
0x02,
0x01,
0x01, // messageID MessageID
0x50,
0x01,
( byte ) 0xFF // CHOICE { ..., abandonRequest AbandonRequest,...
// AbandonRequest ::= [APPLICATION 16] MessageID
} );
stream.flip();
// Allocate a LdapMessageContainer Container
LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer =
new LdapMessageContainer<MessageDecorator<? extends Message>>( codec );
// Decode the PDU
try
{
ldapDecoder.decode( stream, ldapMessageContainer );
}
catch ( DecoderException de )
{
assertTrue( true );
return;
}
fail( "We should not reach this point" );
}
示例6: gen0x11Message
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void gen0x11Message(ClientMessage cm, ArrayList<ServerMessage> smList) throws Exception{
if(message0x11 == 0){
return;
}
byte[] data = new byte[Constant.SERVER_MESSAGE_MIN_LENGTH+8];//13 bytes
ByteBuffer bb = ByteBuffer.wrap(data);
bb.put((byte)1);//version
bb.put(cm.getData()[1]);//app id
bb.put((byte)ClientStatMachine.CMD_0x11);//cmd
bb.putShort((short)8);//length 8
bb.putLong(message0x11);
bb.flip();
ServerMessage sm = new ServerMessage(cm.getSocketAddress(), data);
smList.add(sm);
}
示例7: testEncodePagedSearchControlNegativeSize
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test encoding of a PagedSearchControl with a negative size
*/
@Test
public void testEncodePagedSearchControlNegativeSize() throws Exception
{
ByteBuffer bb = ByteBuffer.allocate( 0x0b );
bb.put( new byte[]
{
0x30, 0x09, // realSearchControlValue ::= SEQUENCE {
0x02,
0x01,
( byte ) 0xFF, // size INTEGER,
0x04,
0x04,
't',
'e',
's',
't' // cookie OCTET STRING,
} );
bb.flip();
PagedResultsDecorator decorator = new PagedResultsDecorator( codec );
PagedResults pagedSearch = ( PagedResults ) decorator.decode( bb.array() );
assertEquals( Integer.MAX_VALUE, pagedSearch.getSize() );
assertTrue( Arrays.equals( Strings.getBytesUtf8( "test" ),
pagedSearch.getCookie() ) );
bb.flip();
PagedResultsDecorator ctrl = new PagedResultsDecorator( codec );
ctrl.setSize( -1 );
ctrl.setCookie( Strings.getBytesUtf8( "test" ) );
ByteBuffer buffer = ctrl.encode( ByteBuffer.allocate( ctrl.computeLength() ) );
String decoded = Strings.dumpBytes( buffer.array() );
String expected = Strings.dumpBytes( bb.array() );
assertEquals( expected, decoded );
}
示例8: doIt
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(1024);
int total = 0;
for (;;) {
bb.clear();
int n = sc.read(bb);
if (n < 0) {
break;
}
total += n;
// echo
bb.flip();
sc.write(bb);
// close after X bytes?
if (closeAfter > 0 && total >= closeAfter) {
break;
}
}
sc.close();
if (delay > 0) {
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException x) { }
}
}
示例9: main
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
for (int x=0; x<100; x++) {
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
Pipe.SinkChannel sink = p.sink();
Pipe.SourceChannel source = p.source();
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(10);
byte[] someBytes = new byte[10];
generator.nextBytes(someBytes);
outgoingdata.put(someBytes);
outgoingdata.flip();
int totalWritten = 0;
while (totalWritten < 10) {
int written = sink.write(outgoingdata);
if (written < 0)
throw new Exception("Write failed");
totalWritten += written;
}
ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
int totalRead = 0;
do {
int bytesRead = source.read(incomingdata);
if (bytesRead > 0)
totalRead += bytesRead;
} while(totalRead < 10);
for(int i=0; i<10; i++)
if (outgoingdata.get(i) != incomingdata.get(i))
throw new Exception("Pipe failed");
sink.close();
source.close();
}
}
示例10: testDecodeSubEntryVisibilityFalse
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test the decoding of a SubEntryControl with a false visibility
*/
@Test
public void testDecodeSubEntryVisibilityFalse() throws Exception
{
ByteBuffer bb = ByteBuffer.allocate( 0x03 );
bb.put( new byte[]
{
0x01, 0x01, 0x00 // Visibility ::= BOOLEAN
} );
bb.flip();
SubentriesDecorator decorator = new SubentriesDecorator( codec );
Subentries subentries = ( Subentries ) decorator.decode( bb.array() );
assertFalse( subentries.isVisible() );
// test encoding
try
{
ByteBuffer buffer = decorator.encode( ByteBuffer.allocate( decorator.computeLength() ) );
String expected = Strings.dumpBytes( bb.array() );
String decoded = Strings.dumpBytes( buffer.array() );
assertEquals( expected, decoded );
}
catch ( EncoderException e )
{
fail( e.getMessage() );
}
}
示例11: handleWrite
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void handleWrite(SelectionKey key) throws IOException {
ByteBuffer buf = (ByteBuffer) key.attachment();
buf.flip();
SocketChannel sc = (SocketChannel) key.channel();
while (buf.hasRemaining()) {
sc.write(buf);
}
buf.compact();
}
示例12: createIndexBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
static short createIndexBuffer(ByteBuffer buffer, int[] indices) {
for (int idx : indices) {
buffer.putShort((short) idx);
}
if (buffer.remaining() != 0) {
throw new RuntimeException("ByteBuffer size and number of arguments do not match");
}
buffer.flip();
BGFXMemory ibhMem = bgfx_make_ref(buffer);
return bgfx_create_index_buffer(ibhMem, BGFX_BUFFER_NONE);
}
示例13: genericTest
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static void genericTest() throws Exception {
StringBuffer sb = new StringBuffer();
sb.setLength(4);
blah = File.createTempFile("blah", null);
blah.deleteOnExit();
initTestFile(blah);
RandomAccessFile raf = new RandomAccessFile(blah, "rw");
FileChannel c = raf.getChannel();
for (int x=0; x<100; x++) {
long offset = generator.nextInt(1000);
ByteBuffer bleck = ByteBuffer.allocateDirect(4);
// Write known sequence out
for (byte i=0; i<4; i++) {
bleck.put(i);
}
bleck.flip();
long originalPosition = c.position();
int totalWritten = 0;
while (totalWritten < 4) {
int written = c.write(bleck, offset);
if (written < 0)
throw new Exception("Read failed");
totalWritten += written;
}
long newPosition = c.position();
// Ensure that file pointer position has not changed
if (originalPosition != newPosition)
throw new Exception("File position modified");
// Attempt to read sequence back in
bleck = ByteBuffer.allocateDirect(4);
originalPosition = c.position();
int totalRead = 0;
while (totalRead < 4) {
int read = c.read(bleck, offset);
if (read < 0)
throw new Exception("Read failed");
totalRead += read;
}
newPosition = c.position();
// Ensure that file pointer position has not changed
if (originalPosition != newPosition)
throw new Exception("File position modified");
for (byte i=0; i<4; i++) {
if (bleck.get(i) != i)
throw new Exception("Write test failed");
}
}
c.close();
raf.close();
blah.delete();
}
示例14: transferTo
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void transferTo(WritableByteChannel out, long position, long size)
throws ApfloatRuntimeException
{
try
{
if (out instanceof FileChannel)
{
// Optimized transferTo() between two FileChannels
while (size > 0)
{
long count = getFileChannel().transferTo(position, size, out);
position += count;
size -= count;
assert (size >= 0);
}
}
else
{
// The DiskChannel transferTo() uses an 8kB buffer, which is too small and inefficient
// So we use a similar mechanism but with a custom buffer size
ByteBuffer buffer = getDirectByteBuffer();
while (size > 0)
{
buffer.clear();
int readCount = (int) Math.min(size, buffer.capacity());
buffer.limit(readCount);
readCount = getFileChannel().read(buffer, position);
buffer.flip();
while (readCount > 0)
{
int writeCount = out.write(buffer);
position += writeCount;
size -= writeCount;
readCount -= writeCount;
}
assert (readCount == 0);
assert (size >= 0);
}
}
}
catch (IOException ioe)
{
throw new BackingStorageException("Unable to read from file \"" + getFilename() + '\"', ioe);
}
}
示例15: testDecodePasswordModifyRequestUserIdentityValueOldPasswordValueNewPasswordValue
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Test the decoding of a PasswordModifyRequest with a user identity, and oldPassword and
* and a newPassword
*/
@Test
public void testDecodePasswordModifyRequestUserIdentityValueOldPasswordValueNewPasswordValue()
{
Asn1Decoder decoder = new Asn1Decoder();
ByteBuffer bb = ByteBuffer.allocate( 0x14 );
bb.put( new byte[]
{ 0x30, 0x12, // PasswordModifyRequest ::= SEQUENCE {
( byte ) 0x80,
0x04, // userIdentity [0] OCTET STRING OPTIONAL
'a',
'b',
'c',
'd',
( byte ) 0x81,
0x04, // oldPassword [1] OCTET STRING OPTIONAL
'e',
'f',
'g',
'h',
( byte ) 0x82, // newPassword [2] OCTET STRING OPTIONAL
0x04,
'i',
'j',
'k',
'l'
} );
String decodedPdu = Strings.dumpBytes( bb.array() );
bb.flip();
PasswordModifyRequestContainer container = new PasswordModifyRequestContainer();
try
{
decoder.decode( bb, container );
}
catch ( DecoderException de )
{
de.printStackTrace();
fail( de.getMessage() );
}
PasswordModifyRequest pwdModifyRequest = container.getPwdModifyRequest();
assertNotNull( pwdModifyRequest.getUserIdentity() );
assertEquals( "abcd", Strings.utf8ToString( pwdModifyRequest.getUserIdentity() ) );
assertNotNull( pwdModifyRequest.getOldPassword() );
assertEquals( "efgh", Strings.utf8ToString( pwdModifyRequest.getOldPassword() ) );
assertNotNull( pwdModifyRequest.getNewPassword() );
assertEquals( "ijkl", Strings.utf8ToString( pwdModifyRequest.getNewPassword() ) );
// Check the length
assertEquals( 0x14, ( ( PasswordModifyRequestDecorator ) pwdModifyRequest ).computeLengthInternal() );
// Check the encoding
try
{
ByteBuffer bb1 = ( ( PasswordModifyRequestDecorator ) pwdModifyRequest ).encodeInternal();
String encodedPdu = Strings.dumpBytes( bb1.array() );
assertEquals( encodedPdu, decodedPdu );
}
catch ( EncoderException ee )
{
ee.printStackTrace();
fail( ee.getMessage() );
}
}