/** * Set the value to a copy of the given byte range * @param newData the new values to copy in * @param offset the offset in newData to start at * @param length the number of bytes to copy */ public void set(byte[] newData, int offset, int length) { setSize(0); setSize(length); System.arraycopy(newData, offset, bytes, 0, size); }
public int next(Writable row) throws IOException { int recordLength = in.read(bytes.get(), 0, maxRecordLength); if (recordLength >= 0) { bytes.setSize(recordLength); } return recordLength; }
public int next(Writable row) throws IOException { int recordLength = in.read(bytes.get(), 0, maxRecordLength); if (recordLength >= 0) { bytes.setSize(recordLength); } return recordLength; }
private static BytesWritable bytes(int... items) { BytesWritable result = new BytesWritable(); result.setSize(items.length); for(int i=0; i < items.length; ++i) { result.getBytes()[i] = (byte) items[i]; } return result; }
private static BytesWritable bytes(int... items) { BytesWritable result = new BytesWritable(); result.setSize(items.length); for (int i = 0; i < items.length; ++i) { result.getBytes()[i] = (byte) items[i]; } return result; }
/** * Writing the value to the output stream. This method avoids copying * value data from Scanner into user buffer, then writing to the output * stream. It does not require the value length to be known. * * @param out * The output stream * @return the length of the value * @throws IOException */ public long writeValue(OutputStream out) throws IOException { DataInputStream dis = getValueStream(); long size = 0; try { int chunkSize; while ((chunkSize = valueBufferInputStream.getRemain()) > 0) { chunkSize = Math.min(chunkSize, MAX_VAL_TRANSFER_BUF_SIZE); valTransferBuffer.setSize(chunkSize); dis.readFully(valTransferBuffer.getBytes(), 0, chunkSize); out.write(valTransferBuffer.getBytes(), 0, chunkSize); size += chunkSize; } return size; } finally { dis.close(); } }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); if (value == null) { return null; } ByteBuffer encoded; if (encoder != null){ try { encoded = encoder.encode(CharBuffer.wrap(value)); } catch (CharacterCodingException e) { throw new HiveException(e); } } else { encoded = Charset.forName( PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); } result.setSize(encoded.limit()); encoded.get(result.getBytes(), 0, encoded.limit()); return result; }
/** * Copy the key into BytesWritable. The input BytesWritable will be * automatically resized to the actual key size. * * @param key * BytesWritable to hold the key. * @throws IOException */ public int getKey(BytesWritable key) throws IOException { key.setSize(getKeyLength()); getKey(key.getBytes()); return key.getLength(); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); if (value == null) { return null; } ByteBuffer encoded; if (encoder != null){ try { encoded = encoder.encode(CharBuffer.wrap(value)); } catch (CharacterCodingException e) { throw new HiveException(e); } } else { encoded = Charset.forName( PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); } result.setSize(encoded.limit()); encoded.get(result.getBytes(), 0, encoded.limit()); return result; }
/** * Copy the value into BytesWritable. The input BytesWritable will be * automatically resized to the actual value size. The implementation * directly uses the buffer inside BytesWritable for storing the value. * The call does not require the value length to be known. * * @param value * @throws IOException */ public long getValue(BytesWritable value) throws IOException { DataInputStream dis = getValueStream(); int size = 0; try { int remain; while ((remain = valueBufferInputStream.getRemain()) > 0) { value.setSize(size + remain); dis.readFully(value.getBytes(), size, remain); size += remain; } return value.getLength(); } finally { dis.close(); } }
private void fillValue(BytesWritable o) { int len = valLenRNG.nextInt(); o.setSize(len); int n = 0; while (n < len) { byte[] word = dict[random.nextInt(dict.length)]; int l = Math.min(word.length, len - n); System.arraycopy(word, 0, o.get(), n, l); n += l; } }
@Override public void collect(byte[] key, byte[] value) throws IOException { BytesWritable keyBW = new BytesWritable(), valueBW = new BytesWritable(); keyBW.setSize(key.length); keyBW.set(key, 0, key.length); valueBW.setSize(value.length); valueBW.set(value, 0, value.length); getCollector().collect(keyBW, valueBW); } }
public void next(BytesWritable key) { key.setSize(Math.max(MIN_KEY_LEN, keyLenRNG.nextInt())); random.nextBytes(key.get()); int rnd = 0; if (max != min) { rnd = random.nextInt(max - min); } int n = rnd + min; byte[] b = key.get(); b[0] = (byte) (n >> 24); b[1] = (byte) (n >> 16); b[2] = (byte) (n >> 8); b[3] = (byte) n; } }
tag = keyWritable.get()[size]; keyWritable = new BytesWritable(keyWritable.getBytes(), size); keyWritable.setSize(size);
keyWritable.setSize(size);
tag = keyWritable.get()[size]; keyWritable = new BytesWritable(keyWritable.getBytes(), size); keyWritable.setSize(size);
private void fillKey(BytesWritable o) { int len = keyLenRNG.nextInt(); if (len < MIN_KEY_LEN) len = MIN_KEY_LEN; o.setSize(len); int n = MIN_KEY_LEN; while (n < len) { byte[] word = dict[random.nextInt(dict.length)]; int l = Math.min(word.length, len - n); System.arraycopy(word, 0, o.get(), n, l); n += l; } if (sorted && WritableComparator.compareBytes(lastKey.get(), MIN_KEY_LEN, lastKey .getSize() - MIN_KEY_LEN, o.get(), MIN_KEY_LEN, o.getSize() - MIN_KEY_LEN) > 0) { incrementPrefix(); } System.arraycopy(prefix, 0, o.get(), 0, MIN_KEY_LEN); lastKey.set(o); }
keyWritable.setSize(size);
@Test public void testHash() throws Exception { byte[] owen = "owen".getBytes(); BytesWritable buf = new BytesWritable(owen); assertEquals(4347922, buf.hashCode()); buf.setCapacity(10000); assertEquals(4347922, buf.hashCode()); buf.setSize(0); assertEquals(1, buf.hashCode()); }