/** * Takes a quoted byte array and converts it into an unquoted byte array * For example: given a byte array representing 'abc', it returns a * byte array representing abc * <p> * @param quotedByteArray the quoted byte array * @return Unquoted byte array */ public static byte [] removeQuotesFromByteArray (byte [] quotedByteArray) { if (quotedByteArray == null || quotedByteArray.length < 2 || quotedByteArray[0] != ParseConstants.SINGLE_QUOTE || quotedByteArray[quotedByteArray.length - 1] != ParseConstants.SINGLE_QUOTE) { throw new IllegalArgumentException("removeQuotesFromByteArray needs a quoted byte array"); } else { byte [] targetString = new byte [quotedByteArray.length - 2]; Bytes.putBytes(targetString, 0, quotedByteArray, 1, quotedByteArray.length - 2); return targetString; } }
/** * Returns the filter name given a simple filter expression * <p> * @param filterStringAsByteArray a simple filter expression * @return name of filter in the simple filter expression */ public static byte [] getFilterName (byte [] filterStringAsByteArray) { int filterNameStartIndex = 0; int filterNameEndIndex = 0; for (int i=filterNameStartIndex; i<filterStringAsByteArray.length; i++) { if (filterStringAsByteArray[i] == ParseConstants.LPAREN || filterStringAsByteArray[i] == ParseConstants.WHITESPACE) { filterNameEndIndex = i; break; } } if (filterNameEndIndex == 0) { throw new IllegalArgumentException("Incorrect Filter Name"); } byte [] filterName = new byte[filterNameEndIndex - filterNameStartIndex]; Bytes.putBytes(filterName, 0, filterStringAsByteArray, 0, filterNameEndIndex - filterNameStartIndex); return filterName; }
public static byte[] appendMetaData(byte[] id, byte[] data) { if (data == null || data.length == 0) { return data; } byte[] salt = Bytes.toBytes(ThreadLocalRandom.current().nextLong()); int idLength = id.length + salt.length; byte[] newData = new byte[MAGIC_SIZE + ID_LENGTH_SIZE + idLength + data.length]; int pos = 0; pos = Bytes.putByte(newData, pos, MAGIC); pos = Bytes.putInt(newData, pos, idLength); pos = Bytes.putBytes(newData, pos, id, 0, id.length); pos = Bytes.putBytes(newData, pos, salt, 0, salt.length); pos = Bytes.putBytes(newData, pos, data, 0, data.length); return newData; }
@Override public void setTimestamp(byte[] ts) { Bytes.putBytes(this.bytes, this.getTimestampOffset(), ts, 0, Bytes.SIZEOF_LONG); }
/** * Convert a BigDecimal value to a byte array * * @param val * @return the byte array */ public static byte[] toBytes(BigDecimal val) { byte[] valueBytes = val.unscaledValue().toByteArray(); byte[] result = new byte[valueBytes.length + SIZEOF_INT]; int offset = putInt(result, 0, val.scale()); putBytes(result, offset, valueBytes, 0, valueBytes.length); return result; }
@Override public void setTimestamp(long ts) { Bytes.putBytes(this.bytes, this.getTimestampOffset(), Bytes.toBytes(ts), 0, Bytes.SIZEOF_LONG); }
/** * Put a BigDecimal value out to the specified byte array position. * * @param bytes the byte array * @param offset position in the array * @param val BigDecimal to write out * @return incremented offset */ public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) { if (bytes == null) { return offset; } byte[] valueBytes = val.unscaledValue().toByteArray(); byte[] result = new byte[valueBytes.length + SIZEOF_INT]; offset = putInt(result, offset, val.scale()); return putBytes(result, offset, valueBytes, 0, valueBytes.length); }
private short put(byte[] array, int offset, int length, boolean copy) { if (copy) { // We copy the bytes we want, otherwise we might be holding references to // massive arrays in our dictionary (or those arrays might change) byte[] stored = new byte[length]; Bytes.putBytes(stored, 0, array, offset, length); return putInternal(stored); } else { return putInternal(array); } }
/** * Format for a tag : * {@code <length of tag - 2 bytes><type code - 1 byte><tag>} tag length is serialized * using 2 bytes only but as this will be unsigned, we can have max tag length of * (Short.MAX_SIZE * 2) +1. It includes 1 byte type length and actual tag bytes length. */ public ArrayBackedTag(byte tagType, byte[] tag) { int tagLength = tag.length + TYPE_LENGTH_SIZE; if (tagLength > MAX_TAG_LENGTH) { throw new IllegalArgumentException( "Invalid tag data being passed. Its length can not exceed " + MAX_TAG_LENGTH); } length = TAG_LENGTH_SIZE + tagLength; bytes = new byte[length]; int pos = Bytes.putAsShort(bytes, 0, tagLength); pos = Bytes.putByte(bytes, pos, tagType); Bytes.putBytes(bytes, pos, tag, 0, tag.length); this.type = tagType; }
/** * @return a unique ID incorporating IP address, PID, TID and timer. Might be an overkill... * Note though that new UUID in java by default is just a random number. */ public static byte[] generateClientId() { byte[] selfBytes = getIpAddressBytes(); Long pid = getPid(); long tid = Thread.currentThread().getId(); long ts = System.currentTimeMillis(); byte[] id = new byte[selfBytes.length + ((pid != null ? 1 : 0) + 2) * Bytes.SIZEOF_LONG]; int offset = Bytes.putBytes(id, 0, selfBytes, 0, selfBytes.length); if (pid != null) { offset = Bytes.putLong(id, offset, pid); } offset = Bytes.putLong(id, offset, tid); offset = Bytes.putLong(id, offset, ts); assert offset == id.length; return id; }
public static byte[] concatTags(byte[] tags, Cell cell) { int cellTagsLen = cell.getTagsLength(); if (cellTagsLen == 0) { // If no Tags, return early. return tags; } byte[] b = new byte[tags.length + cellTagsLen]; int pos = Bytes.putBytes(b, 0, tags, 0, tags.length); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToArray(b, ((ByteBufferExtendedCell) cell).getTagsByteBuffer(), ((ByteBufferExtendedCell) cell).getTagsPosition(), pos, cellTagsLen); } else { Bytes.putBytes(b, pos, cell.getTagsArray(), cell.getTagsOffset(), cellTagsLen); } return b; }
private int readIntoArray(byte[] to, int offset, Dictionary dict) throws IOException { byte status = (byte)in.read(); if (status == Dictionary.NOT_IN_DICTIONARY) { // status byte indicating that data to be read is not in dictionary. // if this isn't in the dictionary, we need to add to the dictionary. int length = StreamUtils.readRawVarint32(in); IOUtils.readFully(in, to, offset, length); dict.addEntry(to, offset, length); return length; } else { // the status byte also acts as the higher order byte of the dictionary entry. short dictIdx = StreamUtils.toShort(status, (byte)in.read()); byte[] entry = dict.getEntry(dictIdx); if (entry == null) { throw new IOException("Missing dictionary entry for index " + dictIdx); } // now we write the uncompressed value. Bytes.putBytes(to, offset, entry, 0, entry.length); return entry.length; } }
/** * Send the response for connection header */ private void responseConnectionHeader(RPCProtos.ConnectionHeaderResponse.Builder chrBuilder) throws FatalConnectionException { // Response the connection header if Crypto AES is enabled if (!chrBuilder.hasCryptoCipherMeta()) return; try { byte[] connectionHeaderResBytes = chrBuilder.build().toByteArray(); // encrypt the Crypto AES cipher meta data with sasl server, and send to client byte[] unwrapped = new byte[connectionHeaderResBytes.length + 4]; Bytes.putBytes(unwrapped, 0, Bytes.toBytes(connectionHeaderResBytes.length), 0, 4); Bytes.putBytes(unwrapped, 4, connectionHeaderResBytes, 0, connectionHeaderResBytes.length); byte[] wrapped = saslServer.wrap(unwrapped, 0, unwrapped.length); BufferChain bc; try (ByteBufferOutputStream response = new ByteBufferOutputStream(wrapped.length + 4); DataOutputStream out = new DataOutputStream(response)) { out.writeInt(wrapped.length); out.write(wrapped); bc = new BufferChain(response.getByteBuffer()); } doRespond(() -> bc); } catch (IOException ex) { throw new UnsupportedCryptoException(ex.getMessage(), ex); } }
/** * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}. * @return number of bytes written. */ public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) { Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen); dst.setPosition(dst.getPosition() + vlen); return vlen; } }
for (int c = 0; c < colQualifiersTotal; c++) { byte[] cq = new byte[4]; Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);
/** * Returns tag value in a new byte array. Primarily for use client-side. If server-side, use * {@link Tag#getValueArray()} with appropriate {@link Tag#getValueOffset()} and * {@link Tag#getValueLength()} instead to save on allocations. * @param tag The Tag whose value to be returned * @return tag value in a new byte array. */ public static byte[] cloneValue(Tag tag) { int tagLength = tag.getValueLength(); byte[] tagArr = new byte[tagLength]; if (tag.hasArray()) { Bytes.putBytes(tagArr, 0, tag.getValueArray(), tag.getValueOffset(), tagLength); } else { ByteBufferUtils.copyFromBufferToArray(tagArr, tag.getValueByteBuffer(), tag.getValueOffset(), 0, tagLength); } return tagArr; }
/** * Copies the tag's value bytes to the given byte array * @param tag The Tag * @param out The byte array where to copy the Tag value. * @param offset The offset within 'out' array where to copy the Tag value. */ public static void copyValueTo(Tag tag, byte[] out, int offset) { if (tag.hasArray()) { Bytes.putBytes(out, offset, tag.getValueArray(), tag.getValueOffset(), tag.getValueLength()); } else { ByteBufferUtils.copyFromBufferToArray(out, tag.getValueByteBuffer(), tag.getValueOffset(), offset, tag.getValueLength()); } }
Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);
pos = Bytes.putByte(b, pos, tag.getType()); if (tag.hasArray()) { pos = Bytes.putBytes(b, pos, tag.getValueArray(), tag.getValueOffset(), tlen); } else { ByteBufferUtils.copyFromBufferToArray(b, tag.getValueByteBuffer(), tag.getValueOffset(),
@Test public void testBasic() { Random rand = new Random(); byte[] testBytes = new byte[10]; rand.nextBytes(testBytes); // Verify that our randomly generated array doesn't exist in the dictionary assertEquals(-1, testee.findEntry(testBytes, 0, testBytes.length)); // now since we looked up an entry, we should have added it to the // dictionary, so it isn't empty assertFalse(isDictionaryEmpty(testee)); // Check if we can find it using findEntry short t = testee.findEntry(testBytes, 0, testBytes.length); // Making sure we do find what we're looking for assertTrue(t != -1); byte[] testBytesCopy = new byte[20]; Bytes.putBytes(testBytesCopy, 10, testBytes, 0, testBytes.length); // copy byte arrays, make sure that we check that equal byte arrays are // equal without just checking the reference assertEquals(testee.findEntry(testBytesCopy, 10, testBytes.length), t); // make sure the entry retrieved is the same as the one put in assertTrue(Arrays.equals(testBytes, testee.getEntry(t))); testee.clear(); // making sure clear clears the dictionary assertTrue(isDictionaryEmpty(testee)); }