@Override public void readFields(DataInput in) throws IOException { int size = in.readInt(); byte[] bytes = new byte[size]; in.readFully(bytes, 0, size); ba = new BytesArray(bytes); }
public BytesArray get() { if (inUse < pool.size() - 1) { return pool.get(inUse++); } else { BytesArray ba = new BytesArray(64); pool.add(ba); inUse++; return ba; } }
/** * Creates a new byte array output stream, with a buffer capacity of * the specified size, in bytes. * * @param size the initial size. * @throws EsHadoopIllegalArgumentException if size is negative. */ public FastByteArrayOutputStream(int size) { Assert.isTrue(size >= 0, "Negative initial size: " + size); data = new BytesArray(size); }
public BytesArray pop() { Entry entry = entries.remove(0); size -= entry.length; byte[] entryData = new byte[entry.length]; System.arraycopy(data.bytes(), entry.offset, entryData, 0, entry.length); return new BytesArray(entryData, entry.length); }
private static BytesArray unwrapStreamBuffer(InputStream in) { if (in instanceof FastByteArrayInputStream) { return ((FastByteArrayInputStream) in).data; } if (in instanceof ByteArrayInputStream) { return new BytesArray(byteArrayInputStreamInternalBuffer((ByteArrayInputStream) in)); } return null; }
public static BytesArray asBytes(InputStream in) throws IOException { BytesArray ba = unwrapStreamBuffer(in); if (ba != null) { return ba; } return asBytes(new BytesArray(in.available()), in); }
public boolean deleteScroll(String scrollId) { BytesArray body; if (internalVersion.onOrAfter(EsMajorVersion.V_2_X)) { body = new BytesArray(("{\"scroll_id\":[\"" + scrollId + "\"]}").getBytes(StringUtils.UTF_8)); } else { body = new BytesArray(scrollId.getBytes(StringUtils.UTF_8)); } Request req = new SimpleRequest(DELETE, null, "_search/scroll", body); Response res = executeNotFoundAllowed(req); return (res.status() == HttpStatus.OK ? true : false); }
public String toString() { BytesArray ba = new BytesArray(length()); copyTo(ba); return ba.toString(); } }
@Before public void setup() { BytesArray all = new BytesArray(new byte[]{1, 2, 3, 4, 5, 6}); BytesArray middleFour = new BytesArray(all.bytes, 1, 4); whole = new FastByteArrayInputStream(all); middle = new FastByteArrayInputStream(middleFour); }
@Test public void testWriteAfterAdding() throws Exception { data.copyFrom(new BytesArray("a")); data.copyFrom(new BytesArray("bb")); ByteArrayOutputStream out = new ByteArrayOutputStream(); data.writeTo(out); assertEquals("abb", out.toString()); }
@Test public void testWriteAfterRemoving() throws Exception { data.copyFrom(new BytesArray("a")); data.copyFrom(new BytesArray("bb")); data.copyFrom(new BytesArray("ccc")); data.remove(1); ByteArrayOutputStream out = new ByteArrayOutputStream(); data.writeTo(out); assertEquals("accc", out.toString()); }
@Test public void testInvalidRequest() throws Exception { BytesArray be = new BytesArray("{\"index\":{\"_index\":\"mroldapi\",\"_type\":\"pattern-format-2179-10-06-with-id\"\"_id\":\"185\"}}" + "{\"url\":\"http://www.last.fm/music/Reamonn\",\"@timestamp\":\"2179-10-06T19:20:25.000Z\",\"name\":\"Reamonn\",\"number\":\"185\",\"picture\":\"http://userserve-ak.last.fm/serve/252/45094837.png\"}"); String error = "JsonParseException[Unexpected character ('\"' (code 34)): was expecting comma to separate OBJECT entries at [Source: [B@6e26dd20; line: 1, column: 75]]"; assertEquals("0-06-with-id\"\"_id\":\"", ErrorUtils.extractJsonParse(error, be)); } }
@Test public void createKeystoreExistsAlreadyOverwrite() throws Exception { TestPrompt console = new TestPrompt(); console.addInput("y"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.CREATE, true, new BytesArray(128)); assertThat(keytool.run(null, false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void testAddRefSize() throws Exception { BytesRef ref = new BytesRef(); ref.add(new BytesArray("one")); ref.add(new BytesArray("three")); data.copyFrom(ref); assertEquals(8, data.length()); }
@Test public void removeNonExistant() throws Exception { TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.REMOVE, false, new BytesArray(128)); assertThat(keytool.run("test.password.1", false, false), equalTo(5)); assertThat(console.getOutput(), equalTo("ERROR: ES-Hadoop keystore not found. Use 'create' command to create one.\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(false)); assertThat(keytool.fileBytes.length(), is(0)); }
@Test public void listKeystoreNonExistant() throws Exception { TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.LIST, false, new BytesArray(128)); assertThat(keytool.run(null, false, false), equalTo(5)); assertThat(console.getOutput(), equalTo("ERROR: ES-Hadoop keystore not found. Use 'create' command to create one.\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(false)); assertThat(keytool.fileBytes.length(), is(0)); }
@Test public void createKeystoreExistsAlreadyCancel() throws Exception { TestPrompt console = new TestPrompt(); console.addInput("n"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.CREATE, true, new BytesArray(128)); assertThat(keytool.run(null, false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("Exiting without creating keystore\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(0)); }
@Test public void addNonExistant() throws Exception { TestPrompt console = new TestPrompt(); console.addInput("blah"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, false, new BytesArray(128)); assertThat(keytool.run("test.password.1", false, false), equalTo(5)); assertThat(console.getOutput(), equalTo("ERROR: ES-Hadoop keystore not found. Use 'create' command to create one.\n")); assertThat(console.hasInputLeft(), is(true)); assertThat(keytool.ksExists(), is(false)); assertThat(keytool.fileBytes.length(), is(0)); }
@Test public void listKeystoreEmpty() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper.newStore().build().saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.LIST, true, storeData); assertThat(keytool.run(null, false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void indexAndType() { Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.LATEST); settings.setResourceWrite("test/{field}"); JsonFieldExtractors jsonFieldExtractors = new JsonFieldExtractors(settings); String data = "{\"field\":\"data\"}"; BytesArray bytes = new BytesArray(data); jsonFieldExtractors.process(bytes); assertThat(jsonFieldExtractors.indexAndType().hasPattern(), is(true)); assertThat(jsonFieldExtractors.indexAndType().field(data).toString(), equalTo("\"_index\":\"test\",\"_type\":\"data\"")); }