Example usage for org.apache.commons.lang3 StringUtils repeat

List of usage examples for org.apache.commons.lang3 StringUtils repeat

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils repeat.

Prototype

public static String repeat(final char ch, final int repeat) 

Source Link

Document

<p>Returns padding using the specified delimiter repeated to a given length.</p> <pre> StringUtils.repeat('e', 0) = "" StringUtils.repeat('e', 3) = "eee" StringUtils.repeat('e', -2) = "" </pre> <p>Note: this method doesn't not support padding with <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a> as they require a pair of char s to be represented.

Usage

From source file:org.apache.nifi.processors.standard.TestPostHTTP.java

@Test
public void testSendWithThrottler() throws Exception {
    setup(null);//from  w  w w .j av a2s .  co m

    final String suppliedMimeType = "text/plain";
    runner.setProperty(PostHTTP.URL, server.getUrl());
    runner.setProperty(PostHTTP.CONTENT_TYPE, suppliedMimeType);
    runner.setProperty(PostHTTP.CHUNKED_ENCODING, "false");
    runner.setProperty(PostHTTP.MAX_DATA_RATE, "10kb");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put(CoreAttributes.MIME_TYPE.key(), "text/plain");

    runner.enqueue(StringUtils.repeat("This is a line of sample text. Here is another.", 100).getBytes(),
            attrs);

    boolean stopOnFinish = true;
    runner.run(1, stopOnFinish);
    runner.assertAllFlowFilesTransferred(PostHTTP.REL_SUCCESS);

    byte[] postValue = servlet.getLastPost();
    Assert.assertArrayEquals(
            StringUtils.repeat("This is a line of sample text. Here is another.", 100).getBytes(), postValue);

    Map<String, String> lastPostHeaders = servlet.getLastPostHeaders();
    Assert.assertEquals(suppliedMimeType, lastPostHeaders.get(PostHTTP.CONTENT_TYPE_HEADER));
    Assert.assertEquals("4700", lastPostHeaders.get("Content-Length"));
}

From source file:org.apache.nifi.provenance.TestStandardRecordReaderWriter.java

@Test
public void testWriteUtfLargerThan64k() throws IOException, InterruptedException {

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("filename", "1.txt");
    attributes.put("uuid", UUID.randomUUID().toString());

    final ProvenanceEventBuilder builder = new StandardProvenanceEventRecord.Builder();
    builder.setEventTime(System.currentTimeMillis());
    builder.setEventType(ProvenanceEventType.RECEIVE);
    builder.setTransitUri("nifi://unit-test");
    builder.fromFlowFile(createFlowFile(3L, 3000L, attributes));
    builder.setComponentId("1234");
    builder.setComponentType("dummy processor");
    final String seventyK = StringUtils.repeat("X", 70000);
    assertTrue(seventyK.length() > 65535);
    assertTrue(seventyK.getBytes("UTF-8").length > 65535);
    builder.setDetails(seventyK);/*www  .j a va 2 s.co m*/
    final ProvenanceEventRecord record = builder.build();

    try (final ByteArrayOutputStream headerOut = new ByteArrayOutputStream();
            final DataOutputStream out = new DataOutputStream(headerOut)) {
        out.writeUTF(PersistentProvenanceRepository.class.getName());
        out.writeInt(9);
    }

    try (final ByteArrayOutputStream recordOut = new ByteArrayOutputStream();
            final StandardRecordWriter writer = new StandardRecordWriter(recordOut, "devnull", idGenerator,
                    null, false, 0)) {

        writer.writeHeader(1L);
        recordOut.reset();

        writer.writeRecord(record);
    }
}

From source file:org.apache.nifi.repository.schema.TestSchemaRecordReaderWriter.java

@Test
@SuppressWarnings("unchecked")
public void testUTFLargerThan64k() throws IOException {
    // Create a Record Schema
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new SimpleRecordField("int present", FieldType.INT, Repetition.ZERO_OR_ONE));
    fields.add(new SimpleRecordField("string present", FieldType.STRING, Repetition.ZERO_OR_ONE));

    final RecordSchema schema = new RecordSchema(fields);

    // Create a Map of record fields to values, so that we can create a Record to write out
    final Map<RecordField, Object> values = new LinkedHashMap<>();
    values.put(createField("int present", FieldType.INT), 42);
    final String utfString = utfStringOneByte + utfStringTwoByte + utfStringThreeByte;  // 3 chars and 6 utf8 bytes
    final String seventyK = StringUtils.repeat(utfString, 21845);  // 65,535 chars and 131070 utf8 bytes
    assertTrue(seventyK.length() == 65535);
    assertTrue(seventyK.getBytes("UTF-8").length == 131070);
    values.put(createField("string present", FieldType.STRING), seventyK);

    final FieldMapRecord originalRecord = new FieldMapRecord(values, schema);

    // Write out a record and read it back in.
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        // Write the schema to the stream
        schema.writeTo(baos);//from  w  ww .j  av  a  2  s  .c o m

        // Write the record twice, to make sure that we're able to read/write multiple sequential records
        final SchemaRecordWriter writer = new SchemaRecordWriter();
        writer.writeRecord(originalRecord, baos);
        writer.writeRecord(originalRecord, baos);

        try (final InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
            // Read the Schema from the stream and create a Record Reader for reading records, based on this schema
            final RecordSchema readSchema = RecordSchema.readFrom(in);
            final SchemaRecordReader reader = SchemaRecordReader.fromSchema(readSchema);

            // Read the records and verify the values.
            for (int i=0; i < 2; i++) {
                final Record record = reader.readRecord(in);

                assertNotNull(record);
                assertEquals(42, record.getFieldValue("int present"));
                assertTrue(MAX_ALLOWED_UTF_LENGTH - ((String)record.getFieldValue("string present")).getBytes("utf-8").length <= 3);
                assertEquals(32768, ((String)record.getFieldValue("string present")).length());
            }

            // Ensure that there is no more data.
            assertNull(reader.readRecord(in));
        }
    }
}

From source file:org.apache.nifi.repository.schema.TestSchemaRecordReaderWriter.java

@Test
public void testSmallCharUTFLengths() throws UnsupportedEncodingException {
    final String string12b = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 2);

    assertEquals("test multi-char string truncated to  0 utf bytes should be 0", 0, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  0));
    assertEquals("test multi-char string truncated to  1 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  1));
    assertEquals("test multi-char string truncated to  2 utf bytes should be 0", 1, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  2));
    assertEquals("test multi-char string truncated to  3 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  3));
    assertEquals("test multi-char string truncated to  4 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  4));
    assertEquals("test multi-char string truncated to  5 utf bytes should be 0", 2, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  5));
    assertEquals("test multi-char string truncated to  6 utf bytes should be 0", 3, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  6));
    assertEquals("test multi-char string truncated to  7 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  7));
    assertEquals("test multi-char string truncated to  8 utf bytes should be 0", 4, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  8));
    assertEquals("test multi-char string truncated to  9 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b,  9));
    assertEquals("test multi-char string truncated to 10 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 10));
    assertEquals("test multi-char string truncated to 11 utf bytes should be 0", 5, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 11));
    assertEquals("test multi-char string truncated to 12 utf bytes should be 0", 6, SchemaRecordWriter.getCharsInUTF8Limit(string12b, 12));
}

From source file:org.apache.nifi.repository.schema.TestSchemaRecordReaderWriter.java

@Test
public void testLargeCharUTFLengths() {
    final String string64k = StringUtils.repeat(utfStringOneByte + utfStringTwoByte + utfStringThreeByte, 21845);

    assertEquals("test 64k char string should be 64k chars long", 65535, string64k.length());

    // drop half the chars going to utf of 64k bytes -- (1+1+1) * 21845 = 65535 chars which converts to (1+2+3) * 21845 = 131070 utf bytes so 1/2 is truncated
    assertEquals("test 64k char string truncated to 65,535 utf bytes should be 32768", 32768, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65535));

    // dropping bytes off the end of utf length
    assertEquals("test 64k char string truncated to 65,534 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65534)); // lost 2 byte char
    assertEquals("test 64k char string truncated to 65,533 utf bytes should be 32767", 32767, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65533));
    assertEquals("test 64k char string truncated to 65,532 utf bytes should be 32766", 32766, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65532)); // lost 1 byte char
    assertEquals("test 64k char string truncated to 65,531 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65531)); // lost 3 byte char
    assertEquals("test 64k char string truncated to 65,530 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65530));
    assertEquals("test 64k char string truncated to 65,529 utf bytes should be 32765", 32765, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65529));
    assertEquals("test 64k char string truncated to 65,528 utf bytes should be 32764", 32764, SchemaRecordWriter.getCharsInUTF8Limit(string64k, 65528)); // lost 2 byte char (again)
}

From source file:org.apache.phoenix.query.KeyRangeClipTest.java

private static byte[] getRange(PhoenixConnection pconn, List<Object> startValues) throws SQLException {
    byte[] lowerRange;
    if (startValues == null) {
        lowerRange = KeyRange.UNBOUND;//from w ww. jav  a 2  s .  c o m
    } else {
        String upsertValues = StringUtils.repeat("?,", startValues.size()).substring(0,
                startValues.size() * 2 - 1);
        String upsertStmt = "UPSERT INTO T VALUES(" + upsertValues + ")";
        PreparedStatement stmt = pconn.prepareStatement(upsertStmt);
        for (int i = 0; i < startValues.size(); i++) {
            stmt.setObject(i + 1, startValues.get(i));
        }
        stmt.execute();
        Cell startCell = PhoenixRuntime.getUncommittedDataIterator(pconn).next().getSecond().get(0);
        lowerRange = CellUtil.cloneRow(startCell);
        pconn.rollback();
    }
    return lowerRange;
}

From source file:org.apache.reef.io.network.NetworkConnectionServiceTest.java

/**
 * NetworkService messaging rate benchmark.
 *///w w w. java2 s .  c o m
@Test
public void testMessagingNetworkConnServiceRate() throws Exception {

    Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST));

    LOG.log(Level.FINEST, name.getMethodName());

    final int[] messageSizes = { 1, 16, 32, 64, 512, 64 * 1024, 1024 * 1024 };

    for (final int size : messageSizes) {
        final String message = StringUtils.repeat('1', size);
        final int numMessages = 300000 / (Math.max(1, size / 512));
        final Monitor monitor = new Monitor();
        final Codec<String> codec = new StringCodec();
        try (final NetworkMessagingTestService messagingTestService = new NetworkMessagingTestService(
                localAddress)) {
            messagingTestService.registerTestConnectionFactory(groupCommClientId, numMessages, monitor, codec);

            try (final Connection<String> conn = messagingTestService
                    .getConnectionFromSenderToReceiver(groupCommClientId)) {

                final long start = System.currentTimeMillis();
                try {
                    conn.open();
                    for (int count = 0; count < numMessages; ++count) {
                        // send messages to the receiver.
                        conn.write(message);
                    }
                    monitor.mwait();
                } catch (final NetworkException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
                final long end = System.currentTimeMillis();

                final double runtime = ((double) end - start) / 1000;
                LOG.log(Level.INFO, "size: " + size + "; messages/s: " + numMessages / runtime
                        + " bandwidth(bytes/s): " + ((double) numMessages * 2 * size) / runtime); // x2 for unicode chars
            }
        }
    }
}

From source file:org.apache.reef.io.network.NetworkConnectionServiceTest.java

/**
 * NetworkService messaging rate benchmark.
 */// ww w .  jav a 2  s .  co  m
@Test
public void testMessagingNetworkConnServiceRateDisjoint() throws Exception {

    Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST));

    LOG.log(Level.FINEST, name.getMethodName());

    final BlockingQueue<Object> barrier = new LinkedBlockingQueue<>();

    final int numThreads = 4;
    final int size = 2000;
    final int numMessages = 300000 / (Math.max(1, size / 512));
    final int totalNumMessages = numMessages * numThreads;
    final String message = StringUtils.repeat('1', size);

    final ExecutorService e = Executors.newCachedThreadPool();
    for (int t = 0; t < numThreads; t++) {
        final int tt = t;

        e.submit(new Runnable() {
            public void run() {
                try (final NetworkMessagingTestService messagingTestService = new NetworkMessagingTestService(
                        localAddress)) {
                    final Monitor monitor = new Monitor();
                    final Codec<String> codec = new StringCodec();

                    messagingTestService.registerTestConnectionFactory(groupCommClientId, numMessages, monitor,
                            codec);
                    try (final Connection<String> conn = messagingTestService
                            .getConnectionFromSenderToReceiver(groupCommClientId)) {
                        try {
                            conn.open();
                            for (int count = 0; count < numMessages; ++count) {
                                // send messages to the receiver.
                                conn.write(message);
                            }
                            monitor.mwait();
                        } catch (final NetworkException e) {
                            e.printStackTrace();
                            throw new RuntimeException(e);
                        }
                    }
                } catch (final Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    // start and time
    final long start = System.currentTimeMillis();
    final Object ignore = new Object();
    for (int i = 0; i < numThreads; i++) {
        barrier.add(ignore);
    }
    e.shutdown();
    e.awaitTermination(100, TimeUnit.SECONDS);
    final long end = System.currentTimeMillis();
    final double runtime = ((double) end - start) / 1000;
    LOG.log(Level.INFO, "size: " + size + "; messages/s: " + totalNumMessages / runtime
            + " bandwidth(bytes/s): " + ((double) totalNumMessages * 2 * size) / runtime); // x2 for unicode chars
}

From source file:org.apache.reef.io.network.NetworkConnectionServiceTest.java

@Test
public void testMultithreadedSharedConnMessagingNetworkConnServiceRate() throws Exception {

    Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST));

    LOG.log(Level.FINEST, name.getMethodName());

    final int[] messageSizes = { 2000 }; // {1,16,32,64,512,64*1024,1024*1024};

    for (final int size : messageSizes) {
        final String message = StringUtils.repeat('1', size);
        final int numMessages = 300000 / (Math.max(1, size / 512));
        final int numThreads = 2;
        final int totalNumMessages = numMessages * numThreads;
        final Monitor monitor = new Monitor();
        final Codec<String> codec = new StringCodec();
        try (final NetworkMessagingTestService messagingTestService = new NetworkMessagingTestService(
                localAddress)) {//from ww  w .  ja v  a  2 s .com
            messagingTestService.registerTestConnectionFactory(groupCommClientId, totalNumMessages, monitor,
                    codec);

            final ExecutorService e = Executors.newCachedThreadPool();

            try (final Connection<String> conn = messagingTestService
                    .getConnectionFromSenderToReceiver(groupCommClientId)) {

                final long start = System.currentTimeMillis();
                for (int i = 0; i < numThreads; i++) {
                    e.submit(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                conn.open();
                                for (int count = 0; count < numMessages; ++count) {
                                    // send messages to the receiver.
                                    conn.write(message);
                                }
                            } catch (final Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });
                }

                e.shutdown();
                e.awaitTermination(30, TimeUnit.SECONDS);
                monitor.mwait();
                final long end = System.currentTimeMillis();
                final double runtime = ((double) end - start) / 1000;
                LOG.log(Level.INFO, "size: " + size + "; messages/s: " + totalNumMessages / runtime
                        + " bandwidth(bytes/s): " + ((double) totalNumMessages * 2 * size) / runtime); // x2 for unicode chars
            }
        }
    }
}

From source file:org.apache.reef.io.network.NetworkConnectionServiceTest.java

/**
 * NetworkService messaging rate benchmark.
 *///from w w  w.j av  a  2 s  .  c  o  m
@Test
public void testMessagingNetworkConnServiceBatchingRate() throws Exception {

    Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST));

    LOG.log(Level.FINEST, name.getMethodName());

    final int batchSize = 1024 * 1024;
    final int[] messageSizes = { 32, 64, 512 };

    for (final int size : messageSizes) {
        final String message = StringUtils.repeat('1', batchSize);
        final int numMessages = 300 / (Math.max(1, size / 512));
        final Monitor monitor = new Monitor();
        final Codec<String> codec = new StringCodec();
        try (final NetworkMessagingTestService messagingTestService = new NetworkMessagingTestService(
                localAddress)) {
            messagingTestService.registerTestConnectionFactory(groupCommClientId, numMessages, monitor, codec);
            try (final Connection<String> conn = messagingTestService
                    .getConnectionFromSenderToReceiver(groupCommClientId)) {
                final long start = System.currentTimeMillis();
                try {
                    conn.open();
                    for (int i = 0; i < numMessages; i++) {
                        conn.write(message);
                    }
                    monitor.mwait();
                } catch (final NetworkException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }

                final long end = System.currentTimeMillis();
                final double runtime = ((double) end - start) / 1000;
                final long numAppMessages = numMessages * batchSize / size;
                LOG.log(Level.INFO, "size: " + size + "; messages/s: " + numAppMessages / runtime
                        + " bandwidth(bytes/s): " + ((double) numAppMessages * 2 * size) / runtime); // x2 for unicode chars
            }
        }
    }
}