/** * Returns the current size of the buffer. * * @return the value of the <code>count</code> field, which is the number * of valid bytes in this output stream. * @see java.io.ByteArrayOutputStream#count */ public long size() { return data.length(); }
@Override public int getLength() { return (ba != null ? ba.length() : 0); }
@Override public int length() { int size = 0; for (int i = 0; i < inUse; i++) { size += pool.get(i).length(); } return size; }
public void add(BytesArray bytes) { if (list == null) { list = new ArrayList<Object>(); } list.add(bytes); size += bytes.length(); }
@Override public void write(DataOutput out) throws IOException { int size = (ba != null ? ba.length() : 0); byte[] bytes = (ba != null ? ba.bytes() : BytesArray.EMPTY); out.writeInt(size); out.write(bytes, 0, size); }
public void process(BytesArray storage) { // no extractors, no lookups if (ObjectUtils.isEmpty(paths)) { return; } results.clear(); if (log.isTraceEnabled()) { log.trace(String.format("About to look for paths [%s] in doc [%s]", Arrays.toString(paths), storage)); } results.addAll(ParsingUtils.values(new JacksonJsonParser(storage.bytes(), 0, storage.length()), paths)); }
/** * Generate the stream of bytes as hex pairs separated by ' '. */ @Override public String toString() { return (ba != null ? StringUtils.asUTFString(ba.bytes(), 0, ba.length()) : ""); }
@Override public int hashCode() { return WritableComparator.hashBytes(ba.bytes(), ba.length()); }
/** * Writes the objects to index. * * @param ba The data as a bytes array */ public void writeProcessedToIndex(BytesArray ba) { Assert.notNull(ba, "no data given"); Assert.isTrue(ba.length() > 0, "no data given"); lazyInitWriting(); trivialBytesRef.reset(); trivialBytesRef.add(ba); doWriteToIndex(trivialBytesRef); }
@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 createKeystore() throws Exception { TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.CREATE, false, 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 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 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 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 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 removeMissingKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.REMOVE, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(6)); assertThat(console.getOutput(), equalTo("ERROR: Setting [test.password.1] does not exist in the keystore.\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void addExistingKeyCancel() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "blah"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("n"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("Exiting without modifying keystore\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void addKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blahh"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", 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))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.getSecureSetting("test.password.1"), equalTo("blahh")); }
@Test public void testPopData() throws Exception { assertEquals(0, data.length()); data.copyFrom(new BytesArray("a")); data.copyFrom(new BytesArray("bb")); data.copyFrom(new BytesArray("ccc")); data.copyFrom(new BytesArray("dddd")); assertEquals(10, data.length()); BytesArray entry = data.pop(); assertEquals(9, data.length()); assertEquals(1, entry.length()); entry = data.pop(); assertEquals(7, data.length()); assertEquals(2, entry.length()); } }