Example usage for java.nio ByteBuffer limit

List of usage examples for java.nio ByteBuffer limit

Introduction

In this page you can find the example usage for java.nio ByteBuffer limit.

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:com.intel.hadoop.hbase.dot.doc.serializer.AvroData.java

byte[] getValue(String field) {
    Object value = this.record.get(field);
    if (null != value) {
        ByteBuffer buf = (ByteBuffer) value;
        // when reusing record, ByteBuffer size could be larger than actual data
        // need to check the limit and return byte[] only contain the real data.
        return buf.limit() == buf.capacity() ? buf.array() : Bytes.getBytes(buf);
    }//  w w  w .  j a  v a  2  s.c o  m
    return null;
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that does not include any groups.
 *///w  w w .j  av  a 2  s  .  com
@Test
public void testRowKeyWithNoGroup() throws Exception {
    // setup
    measurement.withGroups(Collections.emptyList());

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:com.spectralogic.ds3client.helpers.channels.WindowedSeekableByteChannel_Test.java

@Test(timeout = 1000)
public void writeDoesNotExceedWindow() throws IOException {
    try (final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel()) {
        final Object lock = new Object();
        try (final WindowedSeekableByteChannel window = new WindowedSeekableByteChannel(channel, lock, 2L,
                7L)) {// w w w .  j a  va2s .c o m
            final ByteBuffer buffer = Charset.forName("UTF-8").encode("0123456789");
            buffer.position(1);
            assertThat(window.write(buffer), is(7));
            assertThat(window.position(), is(7L));
            assertThat(buffer.position(), is(8));
            assertThat(buffer.limit(), is(10));

            assertThat(window.write(buffer), is(-1));
            assertThat(window.position(), is(7L));

            assertThat(channel.size(), is(9L));
            channel.position(0);

            assertThat(channel.toString(), is("\0\0" + "1234567"));
        }
    }
}

From source file:org.apache.hadoop.hive.serde2.lazy.fast.LazySimpleSerializeWrite.java

@Override
public void writeHiveVarchar(HiveVarchar hiveVarchar) throws IOException {
    beginPrimitive();/*  w w w .  j a  va  2  s . c  om*/
    ByteBuffer b = Text.encode(hiveVarchar.getValue());
    LazyUtils.writeEscaped(output, b.array(), 0, b.limit(), isEscaped, escapeChar, needsEscape);
    finishPrimitive();
}

From source file:com.slytechs.capture.file.editor.FileEditorImpl.java

public void add(final ByteBuffer b, final long global) throws IOException {
    final long length = b.limit() - b.position();

    // Create a partial loader for our cache memory buffer and do the insert
    final PartialLoader record = new MemoryCacheLoader(b, true, headerReader);
    this.edits.insert(global, length, record);

    this.autoflushChange(length);
}

From source file:org.apache.hadoop.hive.serde2.lazy.fast.LazySimpleSerializeWrite.java

@Override
public void writeHiveChar(HiveChar hiveChar) throws IOException {
    beginPrimitive();//from   www .  jav  a  2  s .  com
    ByteBuffer b = Text.encode(hiveChar.getPaddedValue());
    LazyUtils.writeEscaped(output, b.array(), 0, b.limit(), isEscaped, escapeChar, needsEscape);
    finishPrimitive();
}

From source file:org.alfresco.cacheserver.PatchServiceRESTTest.java

public PatchDocument getPatch(MultiPart resource) throws IOException {
    Integer blockSize = null;/*from w  ww .jav  a2s  .  c  om*/
    Integer matchCount = null;

    List<Integer> matchedBlocks = null;
    List<Patch> patches = new LinkedList<>();

    int c = 0;
    InputStream is = null;
    Integer size = null;
    Integer lastMatchIndex = null;

    // This will iterate the individual parts of the multipart response
    for (BodyPart bodyPart : resource.getBodyParts()) {
        //           if(bodyPart instanceof FormDataMultiPart)
        //           {
        //               System.out.printf(
        //                       "Multipart Body Part [Mime Type: %s]\n",
        //                       bodyPart.getMediaType());

        //               FormDataMultiPart mp = (FormDataMultiPart)bodyPart;
        //               for (BodyPart bodyPart1 : mp.getBodyParts())
        //               {
        ContentDisposition contentDisposition = bodyPart.getContentDisposition();
        //                   if(contentDisposition instanceof FormDataContentDisposition)
        //                   {
        //                       FormDataContentDisposition cd = (FormDataContentDisposition)contentDisposition;
        Map<String, String> parameters = contentDisposition.getParameters();
        String name = parameters.get("name");
        MediaType mediaType = bodyPart.getMediaType();
        //                   System.out.println("Body Part " + name);

        if (name.equals("p_size")) {
            String s = getAsString(bodyPart);
            size = Integer.parseInt(s);
            c++;
        } else if (name.equals("p_last_match_idx")) {
            String s = getAsString(bodyPart);
            lastMatchIndex = Integer.parseInt(s);
            c++;
        } else if (name.equals("p_stream")) {
            BodyPartEntity bpEntity = (BodyPartEntity) bodyPart.getEntity();
            is = bpEntity.getInputStream();
            c++;
        } else if (name.equals("p_block_size")) {
            String s = getAsString(bodyPart);
            blockSize = Integer.parseInt(s);
        } else if (name.equals("p_match_count")) {
            String s = getAsString(bodyPart);
            matchCount = Integer.parseInt(s);
        }

        if (c >= 3) {
            c = 0;
            ByteBuffer bb = ByteBuffer.allocate(1024 * 20); // TODO
            ReadableByteChannel channel = Channels.newChannel(is);
            channel.read(bb);
            bb.flip();
            byte[] buffer = new byte[bb.limit()];
            bb.get(buffer);
            Patch patch = new Patch(lastMatchIndex, size, buffer);
            patches.add(patch);
        }
        //                   }
        //               }

        //               ByteBuffer bb = ByteBuffer.allocate(1024*20); // TODO
        //               ReadableByteChannel channel = Channels.newChannel(is);
        //               channel.read(bb);
        //               bb.flip();
        //               byte[] buffer = new byte[bb.limit()];
        //               bb.get(buffer);
        //               Patch patch = new Patch(lastMatchIndex, size, buffer);
        //               patches.add(patch);
    }
    //           else
    //           {
    //               System.out.printf(
    //                       "Embedded Body Part [Mime Type: %s, Length: %s]\n",
    //                       bodyPart.getMediaType(), bodyPart.getContentDisposition().getSize());
    //
    //               ContentDisposition contentDisposition = bodyPart.getContentDisposition();
    //               Map<String, String> parameters = contentDisposition.getParameters();
    //               String name = parameters.get("name");
    ////               if(contentDisposition instanceof FormDataContentDisposition)
    ////               {
    ////                   FormDataContentDisposition cd = (FormDataContentDisposition)contentDisposition;
    ////                   String name = cd.getName();
    //
    //               Object entity = bodyPart.getEntity();
    ////               if(entity instanceof BodyPartEntity)
    ////               {
    ////                   BodyPartEntity bpEntity = (BodyPartEntity)entity;
    ////                   if(name.equals("p_block_size"))
    ////                   {
    ////                       blockSize = Integer.parseInt((String)entity);
    ////                   }
    ////                   else if(name.equals("p_match_count"))
    ////                   {
    ////                       matchCount = Integer.parseInt((String)bodyPart.getEntity());
    ////                   }
    ////                   else if(name.equals("p_matched_blocks"))
    ////                   {
    ////                       String matchedBlocksStr = (String)bodyPart.getEntity();
    ////                       List<String> l = Arrays.asList(matchedBlocksStr.split(","));
    ////                       matchedBlocks = l.stream()
    ////                               .filter(s -> s != null && !s.equals(""))
    ////                               .map(s -> Integer.parseInt(s))
    ////                               .collect(Collectors.toList());
    ////                   }
    ////               }
    //           }
    //       }

    PatchDocument patchDocument = new PatchDocument(blockSize, matchedBlocks, patches);
    return patchDocument;
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that includes a single group that is an integer.
 *//*from  ww  w.j  a  v a 2s  . c  om*/
@Test
public void testRowKeyWithOneIntegerGroup() throws Exception {
    // setup
    measurement.withGroups(Arrays.asList(200));

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .put("200".getBytes()).putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that includes only one group.
 *///from www.j  a v a 2 s  .c  om
@Test
public void testRowKeyWithOneGroup() throws Exception {
    // setup
    measurement.withGroups(Arrays.asList("group1"));

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .put("group1".getBytes()).putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:edu.tsinghua.lumaqq.qq.packets.Packet.java

/**
 * bufOutPacketbuf?????//from w ww.  ja  v a2  s .  c  o m
 * 
 * @param buf
 *          ByteBuffer
 * @throws PacketParseException
 *          ?
 */
protected Packet(ByteBuffer buf, QQUser user) throws PacketParseException {
    this(buf, buf.limit() - buf.position(), user);
}