Example usage for java.util Arrays copyOfRange

List of usage examples for java.util Arrays copyOfRange

Introduction

In this page you can find the example usage for java.util Arrays copyOfRange.

Prototype

public static boolean[] copyOfRange(boolean[] original, int from, int to) 

Source Link

Document

Copies the specified range of the specified array into a new array.

Usage

From source file:com.baidu.oped.apm.common.buffer.FixedBufferTest.java

@Test
public void testPadBytes() throws Exception {
    int TOTAL_LENGTH = 20;
    int TEST_SIZE = 10;
    int PAD_SIZE = TOTAL_LENGTH - TEST_SIZE;
    Buffer buffer = new FixedBuffer(32);
    byte[] test = new byte[10];

    random.nextBytes(test);/* w w  w .j  a  v a 2 s  .c o m*/

    buffer.putPadBytes(test, TOTAL_LENGTH);

    byte[] result = buffer.getBuffer();
    Assert.assertEquals(result.length, TOTAL_LENGTH);
    Assert.assertTrue("check data",
            Arrays.equals(Arrays.copyOfRange(test, 0, TEST_SIZE), Arrays.copyOfRange(result, 0, TEST_SIZE)));
    byte[] padBytes = new byte[TOTAL_LENGTH - TEST_SIZE];
    Assert.assertTrue("check pad", Arrays.equals(Arrays.copyOfRange(padBytes, 0, TEST_SIZE),
            Arrays.copyOfRange(result, TEST_SIZE, TOTAL_LENGTH)));

}

From source file:com.twitter.elephantbird.pig.piggybank.Invoker.java

private static Class<?>[] dropFirstClass(Class<?>[] original) {
    if (original.length < 2) {
        return new Class[0];
    } else {/* ww w  .jav a  2s  .co m*/
        return Arrays.copyOfRange(original, 1, original.length - 1);
    }
}

From source file:gobblin.runtime.StateStoreBasedWatermarkStorageCli.java

@Override
public void run(String[] args) {
    Options options = new Options();
    options.addOption(HELP);/* w w  w.  ja v  a2 s .c om*/
    options.addOption(ZK);
    options.addOption(JOB_NAME);
    options.addOption(ROOT_DIR);
    options.addOption(WATCH);

    CommandLine cli;
    try {
        CommandLineParser parser = new DefaultParser();
        cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));
    } catch (ParseException pe) {
        System.out.println("Command line parse exception: " + pe.getMessage());
        return;
    }

    if (cli.hasOption(HELP.getOpt())) {
        printUsage(options);
        return;
    }

    TaskState taskState = new TaskState();

    String jobName;
    if (!cli.hasOption(JOB_NAME.getOpt())) {
        log.error("Need Job Name to be specified --", JOB_NAME.getLongOpt());
        throw new RuntimeException("Need Job Name to be specified");
    } else {
        jobName = cli.getOptionValue(JOB_NAME.getOpt());
        log.info("Using job name: {}", jobName);
    }
    taskState.setProp(ConfigurationKeys.JOB_NAME_KEY, jobName);

    String zkAddress = "locahost:2181";
    if (cli.hasOption(ZK.getOpt())) {
        zkAddress = cli.getOptionValue(ZK.getOpt());
    }

    log.info("Using zk address : {}", zkAddress);

    taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_TYPE_KEY, "zk");
    taskState.setProp("state.store.zk.connectString", zkAddress);

    if (cli.hasOption(ROOT_DIR.getOpt())) {
        String rootDir = cli.getOptionValue(ROOT_DIR.getOpt());
        taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_CONFIG_PREFIX
                + ConfigurationKeys.STATE_STORE_ROOT_DIR_KEY, rootDir);
        log.info("Setting root dir to {}", rootDir);
    } else {
        log.error("Need root directory specified");
        printUsage(options);
        return;
    }

    StateStoreBasedWatermarkStorage stateStoreBasedWatermarkStorage = new StateStoreBasedWatermarkStorage(
            taskState);

    final AtomicBoolean stop = new AtomicBoolean(true);

    if (cli.hasOption(WATCH.getOpt())) {
        stop.set(false);
    }
    try {

        if (!stop.get()) {
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    stop.set(true);
                }
            });
        }
        do {
            boolean foundWatermark = false;
            try {
                for (CheckpointableWatermarkState wmState : stateStoreBasedWatermarkStorage
                        .getAllCommittedWatermarks()) {
                    foundWatermark = true;
                    System.out.println(wmState.getProperties());
                }
            } catch (IOException ie) {
                Throwables.propagate(ie);
            }

            if (!foundWatermark) {
                System.out.println("No watermarks found.");
            }
            if (!stop.get()) {
                Thread.sleep(1000);
            }
        } while (!stop.get());
    } catch (Exception e) {
        Throwables.propagate(e);
    }
}

From source file:com.bitbreeds.webrtc.sctp.model.SCTPChunk.java

/**
 *
 * @param bytes//from  w  w w  .  j  a  v a2s  .  c  om
 * @return
 */
public static SCTPChunk fromBytes(byte[] bytes) {
    if (bytes.length < 4) {
        throw new IllegalArgumentException("Bytes given are to short to be an SCTP chunk: " + " length: "
                + bytes.length + "  data:" + Hex.encodeHexString(bytes));
    }

    SCTPMessageType type = SCTPMessageType.fromByte(bytes[0]);

    int flags = SignalUtil.unsign(bytes[1]);
    int length = SignalUtil.intFromTwoBytes(Arrays.copyOfRange(bytes, 2, 4));

    Map<SCTPFixedAttributeType, SCTPFixedAttribute> fixedAttr = new HashMap<>();

    ByteRange range = SignalUtil.range(4, 4);
    for (SCTPFixedAttributeType t : type.getFixedTypes()) {
        range = range.lengthFromA(t.getLgt());
        byte[] data = copyRange(bytes, range);
        fixedAttr.put(t, new SCTPFixedAttribute(t, data));
        range = range.plus(t.getLgt());
    }

    Map<SCTPAttributeType, SCTPAttribute> varAttr = new HashMap<>();

    byte[] rest = new byte[] {};
    if (type.isNoVarTypes()) {
        range = range.lengthFromA(length - range.getA());
        rest = SignalUtil.copyRange(bytes, range);
    } else {
        while (bytes.length > range.getB()) {
            range = range.lengthFromA(2);
            SCTPAttributeType tp = SCTPAttributeType.fromInt(intFromTwoBytes(copyRange(bytes, range)));
            range = range.plus(2);

            int lgt = intFromTwoBytes(copyRange(bytes, range));
            lgt = Math.max(lgt - 4, 0); //Lgt data includes type and lenght fields, subtract lgt and type to get data portion

            range = range.plus(2).lengthFromA(lgt);

            byte[] data = copyRange(bytes, range);
            range = range.plus(multipleOfFour(lgt));
            varAttr.put(tp, new SCTPAttribute(tp, data));
        }
    }

    return new SCTPChunk(type, SCTPFlags.fromValue(flags), length, fixedAttr, varAttr, rest);
}

From source file:discovery.DiscoveryExample.java

private static void processCommands(ServiceDiscovery<InstanceDetails> serviceDiscovery,
        Map<String, ServiceProvider<InstanceDetails>> providers, CuratorFramework client) throws Exception {
    // More scaffolding that does a simple command line processor

    printHelp();/* w  ww .  ja v  a 2 s .com*/

    List<ExampleServer> servers = Lists.newArrayList();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        boolean done = false;
        while (!done) {
            System.out.print("> ");

            String command = in.readLine().trim();
            String[] parts = command.split("\\s");
            if (parts.length == 0) {
                continue;
            }
            String operation = parts[0];
            String args[] = Arrays.copyOfRange(parts, 1, parts.length);

            if (operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?")) {
                printHelp();
            } else if (operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit")) {
                done = true;
            } else if (operation.equals("add")) {
                addInstance(args, client, command, servers);
            } else if (operation.equals("delete")) {
                deleteInstance(args, command, servers);
            } else if (operation.equals("random")) {
                listRandomInstance(args, serviceDiscovery, providers, command);
            } else if (operation.equals("list")) {
                listInstances(serviceDiscovery);
            }
        }
    } finally {
        for (ExampleServer server : servers) {
            IOUtils.closeQuietly(server);
        }
    }
}

From source file:com.shuffle.p2p.Bytestring.java

public Bytestring take(int a, int b) {
    int from, to;
    if (a < 0) {
        from = bytes.length - a;/*from w w  w  .  j  a  v a 2 s .  co  m*/
    } else {
        from = a;
    }

    if (b < 0) {
        to = bytes.length - b;
    } else {
        to = b;
    }

    if (to <= from || to > bytes.length || from > bytes.length) {
        throw new IllegalArgumentException();
    }

    return new Bytestring(Arrays.copyOfRange(bytes, from, to));
}

From source file:com.sohu.dc.jobkeeper.helper.NodeManager.java

public synchronized void createPath(CreateMode createMode) {
    if (isClosed()) {
        return;// w ww . j av a2 s  .  c  om
    }
    String[] dirs = dir.split("/");
    for (int i = 0; i < dirs.length - 1; i++) {
        String path = StringUtils.join(Arrays.copyOfRange(dirs, 0, i + 2), "/");
        ensurePathExists(path, createMode);
    }
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Decode lz byte [ ]./*from w ww. j av  a2s .  co  m*/
 *
 * @param data       the data
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] decodeLZ(byte[] data, String dictionary) {
    try {
        Inflater decompresser = new Inflater();
        decompresser.setInput(data, 0, data.length);
        byte[] result = new byte[data.length * 32];
        int resultLength = 0;
        if (!dictionary.isEmpty()) {
            resultLength = decompresser.inflate(result);
            assert (0 == resultLength);
            if (decompresser.needsDictionary()) {
                byte[] bytes = dictionary.getBytes("UTF-8");
                decompresser.setDictionary(bytes);
            }
        }
        resultLength = decompresser.inflate(result);
        decompresser.end();
        return Arrays.copyOfRange(result, 0, resultLength);
    } catch (DataFormatException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.enterprise.adaptor.Daemon.java

@Override
public synchronized void init(DaemonContext context) throws Exception {
    if (this.context != null) {
        throw new IllegalStateException("Already initialized");
    }/*www  .j  av a2s.  c o m*/
    this.context = context;
    String[] args = context.getArguments();
    if (args.length < 1) {
        throw new IllegalArgumentException("Missing argument: adaptor class name");
    }
    Adaptor adaptor = Class.forName(args[0]).asSubclass(Adaptor.class).newInstance();
    args = Arrays.copyOfRange(args, 1, args.length);

    app = Application.daemonMain(adaptor, args);
    app.daemonInit();
}

From source file:com.netflix.imfutility.ttmltostl.stl.StlGsiTest.java

@Test
public void testGsiAll() throws Exception {
    TimedTextObject tto = StlTestUtil.buildTto("10:00:15:10", "10:00:16:00", "text1", "10:00:16:00",
            "10:00:17:00", "text2", "10:00:17:00", "10:01:20:00", "text3");
    byte[][] stl = StlTestUtil.build(tto, StlTestUtil.getMetadataXml());
    byte[] gsi = stl[0];

    assertArrayEquals(new byte[] { 0x38, 0x35, 0x30, // 850
            0x53, 0x54, 0x4c, 0x32, 0x35, 0x2e, 0x30, 0x31, // STL25.01
            0x31, // 1 - teletext
            0x30, 0x30, // 00 - latin
            0x30, 0x39 // 09 - English
    }, Arrays.copyOfRange(gsi, 0, 16));

    assertArrayEquals(new byte[] { 0x50, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x65, // 'Programme' as in metadata.xml
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, Arrays.copyOfRange(gsi, 16, 48));

    assertArrayEquals(new byte[] { 0x45, 0x70, 0x69, 0x73, 0x6f, 0x64, 0x65, // 'Episode' as in metadata.xml
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, Arrays.copyOfRange(gsi, 48, 80));

    byte[] translated = new byte[128];
    Arrays.fill(translated, (byte) 0x20);
    assertArrayEquals(translated, Arrays.copyOfRange(gsi, 80, 208));

    byte[] slr = new byte[16];
    Arrays.fill(slr, (byte) 0x20);
    assertArrayEquals(slr, Arrays.copyOfRange(gsi, 208, 224));

    assertArrayEquals(new byte[] { 0x31, 0x36, 0x30, 0x37, 0x30, 0x38 // creation date as in metadata.xml:
    }, Arrays.copyOfRange(gsi, 224, 230));
    assertArrayEquals(new byte[] { 0x31, 0x36, 0x30, 0x37, 0x30, 0x38 // revision date as in metadata.xml:
    }, Arrays.copyOfRange(gsi, 230, 236));

    assertArrayEquals(new byte[] { 0x30, 0x31 // revision number = 01
    }, Arrays.copyOfRange(gsi, 236, 238));

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x30, 0x30, 0x34 // number of tti blocks - 3 + subtitle zero = 4
    }, Arrays.copyOfRange(gsi, 238, 243));

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x30, 0x30, 0x34 // number of subtitles - 3 + subtitle zero = 4
    }, Arrays.copyOfRange(gsi, 243, 248));

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x31 // number of subtitle groups - 1
    }, Arrays.copyOfRange(gsi, 248, 251));

    assertArrayEquals(new byte[] { 0x35, 0x38 // number of displayable chars - 58
    }, Arrays.copyOfRange(gsi, 251, 253));

    assertArrayEquals(new byte[] { 0x31, 0x31 // number of displayable rows - 11
    }, Arrays.copyOfRange(gsi, 253, 255));

    assertArrayEquals(new byte[] { 0x31 // tc status - 1
    }, Arrays.copyOfRange(gsi, 255, 256));

    assertArrayEquals(new byte[] { 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x30 // start time of program: 10:00:00:20 (as in metadata.xml)
    }, Arrays.copyOfRange(gsi, 256, 264));

    assertArrayEquals(new byte[] { 0x31, 0x30, 0x30, 0x30, 0x31, 0x35, 0x31, 0x30 // start time of subtitles: 10:00:15:10 (as in metadata.xml)
    }, Arrays.copyOfRange(gsi, 264, 272));

    assertArrayEquals(new byte[] { 0x31 // number of disks - 1
    }, Arrays.copyOfRange(gsi, 272, 273));

    assertArrayEquals(new byte[] { 0x31 // disk seq number - 1
    }, Arrays.copyOfRange(gsi, 273, 274));

    assertArrayEquals(new byte[] { 0x47, 0x42, 0x52 // country - GBR
    }, Arrays.copyOfRange(gsi, 274, 277));

    assertArrayEquals(new byte[] { 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, // Publisher: Originator (as in metadata.xml)
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, Arrays.copyOfRange(gsi, 277, 309));

    assertArrayEquals(new byte[] { 0x44, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x6f, 0x72, // Editor: Distributor (as in metadata.xml)
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20 }, Arrays.copyOfRange(gsi, 309, 341));

    assertArrayEquals(new byte[] { 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x40, // Contact: account@myemail.com (as in metadata.xml)
            0x6d, 0x79, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x20, 0x20, 0x20, 0x20, 0x20,
            0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, Arrays.copyOfRange(gsi, 341, 373));

    byte[] spare = new byte[651];
    Arrays.fill(spare, (byte) 0x20);
    assertArrayEquals(spare, Arrays.copyOfRange(gsi, 373, 1024));

}