/** * Gets copy of internal array. * * @return Copy of internal array. */ public byte[] array() { byte[] res = new byte[size]; U.arrayCopy(data, 0, res, 0, size); return res; }
/** * Sets bytes from specified buffer to a given value. * * @param buf Buffer. * @param off Offset. * @param len Length. */ public void putBytes(byte[] buf, int off, int len) { if (arr == null) arr = new byte[PACKET_SIZE]; U.arrayCopy(buf, 0, arr, off, len); }
/** * Creates copy of request packet for easy response construction. * * @param req Source request packet. */ GridMemcachedMessage(GridMemcachedMessage req) { assert req != null; reqFlag = req.reqFlag; opCode = req.opCode; opaque = new byte[req.opaque.length]; U.arrayCopy(req.opaque, 0, opaque, 0, req.opaque.length); }
/** {@inheritDoc} */ @Override public int setBytes(long pos, byte[] bytes, int off, int len) throws SQLException { ensureNotClosed(); if (pos < 1) throw new SQLException("Invalid argument. Position can't be less than 1 [pos=" + pos + ']'); int idx = (int)(pos - 1); if (pos - 1 > arr.length || off < 0 || off >= bytes.length || off + len > bytes.length) throw new ArrayIndexOutOfBoundsException(); byte[] dst = arr; if (idx + len > arr.length) { dst = new byte[arr.length + (len - (arr.length - idx))]; U.arrayCopy(arr, 0, dst, 0, idx); arr = dst; } U.arrayCopy(bytes, off, dst, idx, len); return len; }
/** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this byte array output stream. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. */ @Override public void write(byte b[], int off, int len) { if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) throw new IndexOutOfBoundsException(); else if (len == 0) return; int newCnt = cnt + len; if (newCnt > buf.length) buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newCnt)); U.arrayCopy(b, off, buf, cnt, len); cnt = newCnt; }
/** * @param bytes Byte to add. * @param off Offset at which to add. * @param len Number of bytes to add. */ public void add(byte[] bytes, int off, int len) { requestFreeSize(len); U.arrayCopy(bytes, off, data, size, len); size += len; }
/** {@inheritDoc} */ @Override public byte[] getBytes(long pos, int len) throws SQLException { ensureNotClosed(); if (pos < 1 || arr.length - pos < 0 || len < 0) throw new SQLException("Invalid argument. Position can't be less than 1 or " + "greater than size of underlying byte array. Requested length also can't be negative " + "" + "[pos=" + pos + ", len=" + len +']'); int idx = (int)(pos - 1); int size = len > arr.length - idx ? arr.length - idx : len; byte[] res = new byte[size]; U.arrayCopy(arr, idx, res, 0, size); return res; }
return 0; U.arrayCopy(buf, pos, b, off, len);
/** {@inheritDoc} */ @Override public Void process(MutableEntry<IgfsBlockKey, byte[]> entry, Object... args) { byte[] e = entry.getValue(); final int size = data.length; if (e == null || e.length == 0) e = new byte[start + size]; // Don't allocate more, then required. else if (e.length < start + size) { // Expand stored data array, if it less, then required. byte[] tmp = new byte[start + size]; // Don't allocate more than required. U.arrayCopy(e, 0, tmp, 0, e.length); e = tmp; } // Copy data into entry. U.arrayCopy(data, 0, e, start, size); entry.setValue(e); return null; }
/** * @return Serialized checkpoint data. */ public byte[] toBytes() { byte[] keyBytes = key.getBytes(); byte[] bytes = new byte[4 + state.length + 8 + 4 + keyBytes.length]; U.intToBytes(state.length, bytes, 0); U.arrayCopy(state, 0, bytes, 4, state.length); U.longToBytes(expTime, bytes, 4 + state.length); U.intToBytes(keyBytes.length, bytes, 4 + state.length + 8); U.arrayCopy(keyBytes, 0, bytes, 4 + state.length + 8 + 4, keyBytes.length); return bytes; }
U.arrayCopy(block, blockOff, buf, off, len);
/** * Copies cached data if specified position matches cached region. * * @param dst Destination buffer. * @param pos Read position in file. * @param dstOff Offset in destination buffer from which start writing. * @param len Maximum number of bytes to copy. * @return Number of bytes copied. * @throws IgniteCheckedException If read future failed. */ public int flatten(byte[] dst, long pos, int dstOff, int len) throws IgniteCheckedException { // If read start position is within cached boundaries. if (contains(pos)) { byte[] data = readFut.get(); int srcPos = (int)(pos - this.pos); int cpLen = Math.min(len, data.length - srcPos); U.arrayCopy(data, srcPos, dst, dstOff, cpLen); return cpLen; } return 0; }
/** {@inheritDoc} */ @Override public byte[] valueBytes(CacheObjectValueContext ctx) throws IgniteCheckedException { if (detached()) return array(); int len = length(); byte[] arr0 = new byte[len]; U.arrayCopy(arr, start, arr0, 0, len); return arr0; }
/** * @return Detached binary object. */ public BinaryObjectImpl detach() { if (!detachAllowed || detached()) return this; int len = length(); byte[] arr0 = new byte[len]; U.arrayCopy(arr, start, arr0, 0, len); return new BinaryObjectImpl(ctx, arr0, 0); }
byte[] data = new byte[packet.length - 1]; U.arrayCopy(packet, 1, data, 0, data.length);
/** * @throws Exception If failed. */ @Test public void testRegExp() throws Exception { String normal = "swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|b53b3a3d6ab90ce0268229151c9bde11|1315392441288"; byte[] b1 = new byte[200]; byte[] b2 = normal.getBytes(); U.arrayCopy(b2, 0, b1, 30, b2.length); CharSequence corrupt = new String(b1); String ptrn = "[a-z0-9/\\-]+\\|[a-f0-9]+\\|[0-9]+"; Pattern p = Pattern.compile(ptrn); Matcher matcher = p.matcher(corrupt); assert matcher.find(); X.println(String.valueOf(matcher.start())); assert normal.matches(ptrn); } }
/** * Updates meta data. * * @throws SQLException In case of error. */ private void updateMetaData() throws SQLException { if (conn.isClosed()) throw new SQLException("Connection is closed."); try { byte[] packet = conn.client().compute().execute(TASK_NAME, conn.cacheName()); byte status = packet[0]; byte[] data = new byte[packet.length - 1]; U.arrayCopy(packet, 1, data, 0, data.length); if (status == 1) throw JdbcUtils.unmarshalError(data); else { List<Object> res = JdbcUtils.unmarshal(data); meta = (Map<String, Map<String, Map<String, String>>>)res.get(0); indexes = (Collection<List<Object>>)res.get(1); } } catch (GridClientException e) { throw new SQLException("Failed to get meta data from Ignite.", e); } }
/** * @param ctx Context. * @param arr Array. */ public BinaryEnumObjectImpl(BinaryContext ctx, byte[] arr) { assert ctx != null; assert arr != null; if (arr[0] == GridBinaryMarshaller.ENUM) valBytes = arr; else { assert arr[0] == GridBinaryMarshaller.BINARY_ENUM; valBytes = new byte[arr.length]; valBytes[0] = GridBinaryMarshaller.ENUM; U.arrayCopy(arr, 1, valBytes, 1, arr.length - 1); } this.ctx = ctx; int off = 1; this.typeId = BinaryPrimitives.readInt(arr, off); off += 4; if (this.typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { assert arr[off] == GridBinaryMarshaller.STRING; int len = BinaryPrimitives.readInt(arr, ++off); off += 4; byte[] bytes = BinaryPrimitives.readByteArray(arr, off, len); off += len; this.clsName = new String(bytes, UTF_8); } this.ord = BinaryPrimitives.readInt(arr, off); }
/** {@inheritDoc} */ @Override public byte[] reduce(List<ComputeJobResult> results) throws IgniteException { try { byte status; byte[] bytes; ComputeJobResult res = F.first(results); if (res.getException() == null) { status = 0; bytes = U.marshal(MARSHALLER, res.getData()); } else { status = 1; bytes = U.marshal(MARSHALLER, new SQLException(res.getException().getMessage())); } byte[] packet = new byte[bytes.length + 1]; packet[0] = status; U.arrayCopy(bytes, 0, packet, 1, bytes.length); return packet; } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** * */ @Test public void testAddSetInteger() { GridByteArrayList list = new GridByteArrayList(10); int num = 1; list.add(num); assert list.size() == 4; assert Arrays.equals(list.array(), U.intToBytes(num)); int num2 = 2; list.add(num2); int num3 = 3; list.set(4, num3); assert list.size() == 8; byte[] arr2 = new byte[8]; U.arrayCopy(U.intToBytes(num), 0, arr2, 0, 4); U.arrayCopy(U.intToBytes(num3), 0, arr2, 4, 4); assert Arrays.equals(list.array(), arr2); assert list.getInt(0) == num; assert list.getInt(4) == num3; }