List of usage examples for java.nio ByteBuffer limit
public final int limit()
From source file:Main.java
public static final int indexOf(final ByteBuffer bb, final byte b) { return indexOf(bb.array(), 0, bb.limit(), b); }
From source file:com.inmobi.messaging.util.AuditUtil.java
private static boolean isValidSize(ByteBuffer buffer) { int messageSize = buffer.getInt(); if (buffer.limit() != messageSize + HEADER_LENGTH) { LOG.debug("Invalid size of message in headers;found [" + (HEADER_LENGTH + messageSize) + "] expected [" + buffer.limit() + "]"); return false; }/*w w w .java 2 s. c o m*/ return true; }
From source file:mvm.rya.indexing.accumulo.freetext.ColumnPrefixes.java
private static Text concat(Text prefix, String str) { Text temp = new Text(prefix); try {//from w w w . j a va 2s . c om ByteBuffer buffer = Text.encode(str, false); temp.append(buffer.array(), 0, buffer.limit()); } catch (CharacterCodingException cce) { throw new IllegalArgumentException(cce); } return temp; }
From source file:org.apache.hadoop.hive.serde2.lazy.LazyHiveDecimal.java
/** * Writes HiveDecimal object to output stream as string * @param outputStream/* ww w . ja v a2 s.c o m*/ * @param hiveDecimal * @throws IOException */ public static void writeUTF8(OutputStream outputStream, HiveDecimal hiveDecimal) throws IOException { if (hiveDecimal == null) { outputStream.write(nullBytes); } else { ByteBuffer b = Text.encode(hiveDecimal.toString()); outputStream.write(b.array(), 0, b.limit()); } }
From source file:Main.java
public final static boolean equals(final ByteBuffer bb1, final ByteBuffer bb2) { final byte[] array1 = bb1.array(); final byte[] array2 = bb2.array(); final int end1 = bb1.limit(); final int end2 = bb2.limit(); return equals(array1, 0, end1, array2, 0, end2); }
From source file:Main.java
public static ByteBuffer cloneOnHeap(final ByteBuffer buf) { if (buf == null) { return null; }//from ww w. j ava 2 s . c om buf.rewind(); final ByteBuffer copy = createByteBufferOnHeap(buf.limit()); copy.put(buf); return copy; }
From source file:Main.java
public final static void replaceP(final ByteBuffer lineBB, final byte from, final byte to) { final byte[] array = lineBB.array(); final int offset = lineBB.position(); final int limit = lineBB.limit(); replace(array, offset, limit, from, to); }
From source file:Main.java
/** * Returns a new byte array containing the characters of the specified * string encoded using the given charset. * /* w w w.j ava2 s . c o m*/ * It is equivalent to <code>input.getBytes(charset)</code> except it has * workaround for the bug ID 61917. * * @see https://code.google.com/p/android/issues/detail?id=61917 */ //@formatter:off /* * The original code is available from * https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java */ //@formatter:on public static byte[] getBytes(String input, Charset charset) { CharBuffer chars = CharBuffer.wrap(input.toCharArray()); // @formatter:off CharsetEncoder encoder = charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); // @formatter:on ByteBuffer buffer; buffer = encode(chars.asReadOnlyBuffer(), encoder); byte[] bytes = new byte[buffer.limit()]; buffer.get(bytes); return bytes; }
From source file:Main.java
public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> samples) { ArrayList<ByteBuffer> nuSamples = new ArrayList<ByteBuffer>(samples.size()); for (ByteBuffer buffer : samples) { int lastIndex = nuSamples.size() - 1; if (lastIndex >= 0 && buffer.hasArray() && nuSamples.get(lastIndex).hasArray() && buffer.array() == nuSamples.get(lastIndex).array() && nuSamples.get(lastIndex).arrayOffset() + nuSamples.get(lastIndex).limit() == buffer.arrayOffset()) { ByteBuffer oldBuffer = nuSamples.remove(lastIndex); ByteBuffer nu = ByteBuffer .wrap(buffer.array(), oldBuffer.arrayOffset(), oldBuffer.limit() + buffer.limit()).slice(); // We need to slice here since wrap([], offset, length) just sets position and not the arrayOffset. nuSamples.add(nu);/* www . j a v a 2s. c o m*/ } else if (lastIndex >= 0 && buffer instanceof MappedByteBuffer && nuSamples.get(lastIndex) instanceof MappedByteBuffer && nuSamples.get(lastIndex) .limit() == nuSamples.get(lastIndex).capacity() - buffer.capacity()) { // This can go wrong - but will it? ByteBuffer oldBuffer = nuSamples.get(lastIndex); oldBuffer.limit(buffer.limit() + oldBuffer.limit()); } else { buffer.reset(); nuSamples.add(buffer); } } return nuSamples; }
From source file:it.geosolutions.geostore.core.security.password.SecurityUtils.java
/** * Converts a char array to a byte array. *//*ww w . j a va 2 s . c om*/ public static byte[] toBytes(char[] ch, Charset charset) { ByteBuffer buff = charset.encode(CharBuffer.wrap(ch)); byte[] tmp = new byte[buff.limit()]; buff.get(tmp); return tmp; }