Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

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

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:com.netflix.genie.common.model.FileAttachment.java

/**
 * Set the data for the attachment.//from  ww w  . ja  v a 2 s. c  om
 *
 * @param data the data for the attachment. Not null or empty.
 * @throws GeniePreconditionException If preconditions aren't met.
 */
public void setData(final byte[] data) throws GeniePreconditionException {
    if (data == null || data.length == 0) {
        throw new GeniePreconditionException("No data entered for attachment. Unable to continue.");
    }
    this.data = Arrays.copyOf(data, data.length);
}

From source file:net.sf.jasperreports.engine.base.ElementsBlockList.java

public ElementsBlock[] getBlocks() {
    return Arrays.copyOf(blocks, blockCount);
}

From source file:com.splicemachine.pipeline.constraint.ConstraintContext.java

public ConstraintContext withMessage(int index, String newMessage) {
    String[] newArgs = Arrays.copyOf(this.messageArgs, this.messageArgs.length);
    newArgs[index] = newMessage;/* w w w.j a  v  a2 s.c  om*/
    return new ConstraintContext(newArgs);
}

From source file:com.l2jfree.gameserver.model.world.L2WorldRegion.java

public void addZone(L2Zone zone) {
    _zones = Arrays.copyOf(_zones, _zones.length + 1);
    _zones[_zones.length - 1] = zone;
}

From source file:org.kurento.repository.test.RangePutTests.java

protected void uploadFileWithSeqPUTs(RepositoryHttpRecorder recorder, File fileToUpload,
        RepositoryItem repositoryItem) throws Exception {

    recorder.setAutoTerminationTimeout(500000);
    String url = recorder.getURL();

    DataInputStream is = null;/*from  ww w. ja v a 2  s . c o  m*/

    try {

        is = new DataInputStream(new FileInputStream(fileToUpload));

        int sentBytes = 0;

        byte[] info = new byte[40000];

        int readBytes;

        int numRequest = 0;

        while ((readBytes = is.read(info)) != -1) {

            ResponseEntity<String> response = putContent(url, Arrays.copyOf(info, readBytes), sentBytes);

            sentBytes += readBytes;

            log.info(numRequest + ": " + response.toString());

            assertEquals("Returned response: " + response.getBody(), HttpStatus.OK, response.getStatusCode());

            if (numRequest == 3) {

                // Simulating retry

                response = putContent(url, Arrays.copyOf(info, readBytes), sentBytes - readBytes);

                log.info(numRequest + ": " + response.toString());

                assertEquals("Returned response: " + response.getBody(), HttpStatus.OK,
                        response.getStatusCode());

            } else if (numRequest == 4) {

                // Simulating retry with new data

                byte[] newInfo = new byte[500];
                int newReadBytes = is.read(newInfo);

                response = putContent(url,
                        concat(Arrays.copyOf(info, readBytes), Arrays.copyOf(newInfo, newReadBytes)),
                        sentBytes - readBytes);

                sentBytes += newReadBytes;

                log.info(numRequest + ": " + response.toString());

                assertEquals("Returned response: " + response.getBody(), HttpStatus.OK,
                        response.getStatusCode());

            } else if (numRequest == 5) {

                // Simulating send ahead data

                response = putContent(url, Arrays.copyOf(info, readBytes), sentBytes + 75000);

                log.info(numRequest + ": " + response.toString());

                assertEquals("Returned response: " + response.getBody(), HttpStatus.NOT_IMPLEMENTED,
                        response.getStatusCode());

            }

            numRequest++;
        }

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }

        recorder.stop();
    }
}

From source file:com.bitbreeds.webrtc.dtls.DtlsMuxStunTransport.java

public int receive(byte[] buf, int off, int len, int waitMillis) throws IOException {
    socket.setSoTimeout(waitMillis);// www. j  a  va  2s  .  c o m
    DatagramPacket packet = new DatagramPacket(buf, off, len);
    socket.receive(packet);
    logger.trace("Socket read msg: {}",
            Hex.encodeHexString(SignalUtil.copyRange(buf, new ByteRange(0, packet.getLength()))));
    if (buf.length >= 2 && buf[0] == 0 && buf[1] == 1) {
        SocketAddress currentSender = packet.getSocketAddress();

        byte[] data = Arrays.copyOf(packet.getData(), packet.getLength());

        byte[] out = bindingService.processBindingRequest(data, parent.getLocal().getUserName(),
                parent.getLocal().getPassword(), (InetSocketAddress) currentSender);

        logger.trace("Stun packet received, responding with {}", Hex.encodeHexString(out));
        this.send(out, 0, out.length);
        return 0; //We do not want DTLS to process (not that it will anyway), so we return 0 here.
    }

    return packet.getLength();
}

From source file:alfio.manager.system.MailgunMailer.java

private RequestBody prepareBody(Event event, String to, List<String> cc, String subject, String text,
        Optional<String> html, Attachment... attachments) throws IOException {

    String from = event.getDisplayName() + " <" + configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), MAILGUN_FROM)) + ">";

    if (ArrayUtils.isEmpty(attachments)) {
        FormBody.Builder builder = new FormBody.Builder().add("from", from).add("to", to)
                .add("subject", subject).add("text", text);
        if (cc != null && !cc.isEmpty()) {
            builder.add("cc", StringUtils.join(cc, ','));
        }//from w w  w .  ja va2 s .  c o m

        String replyTo = configurationManager.getStringConfigValue(
                Configuration.from(event.getOrganizationId(), event.getId(), MAIL_REPLY_TO), "");
        if (StringUtils.isNotBlank(replyTo)) {
            builder.add("h:Reply-To", replyTo);
        }
        html.ifPresent((htmlContent) -> builder.add("html", htmlContent));
        return builder.build();

    } else {
        MultipartBody.Builder multipartBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);

        multipartBuilder.addFormDataPart("from", from).addFormDataPart("to", to)
                .addFormDataPart("subject", subject).addFormDataPart("text", text);

        if (cc != null && !cc.isEmpty()) {
            multipartBuilder.addFormDataPart("cc", StringUtils.join(cc, ','));
        }

        html.ifPresent((htmlContent) -> multipartBuilder.addFormDataPart("html", htmlContent));

        for (Attachment attachment : attachments) {
            byte[] data = attachment.getSource();
            multipartBuilder.addFormDataPart("attachment", attachment.getFilename(), RequestBody
                    .create(MediaType.parse(attachment.getContentType()), Arrays.copyOf(data, data.length)));
        }
        return multipartBuilder.build();
    }
}

From source file:com.qcadoo.model.internal.api.ValueAndError.java

/**
 * Returns error message arguments (values which will replace message's place holders)
 * //from   www  .  ja  v  a2  s  .c  om
 * @return error message arguments (values which will replace message's place holders)
 */
public String[] getArgs() {
    return Arrays.copyOf(args, args.length);
}

From source file:edu.stevens.cpe.reservoir.translate.HSA.java

/**
 * Decode the spike trains with the current filter.
 * @param spiketrains//from   w w  w .  ja  v  a  2s . c o  m
 */
@Override
public double[] decode(double[] spiketrains) {
    int width = spiketrains.length;//+ filter.length -1;

    double[] output = new double[width];
    //Be non-invasive, make copy
    double[] spikes = Arrays.copyOf(spiketrains, spiketrains.length); //ArrayUtils.pad(spiketrains, filter.length, true);//new double [width];
    ArrayUtils.reverse(spikes);
    int shiftIndex = spikes.length - 1;
    for (int k = 0; k < width; k++) {
        double sum = 0;

        for (int j = 0; j < filter.length; j++) {
            if (shiftIndex + j < spikes.length) {// && shiftIndex + j >=0 ){
                sum += spikes[shiftIndex + j] * filter[j];
            } else {
                break;
            }
        }

        output[k] = sum;
        shiftIndex--;
    }

    return output;
}

From source file:net.sf.taverna.t2.activities.wsdl.xmlsplitter.XMLSplitterConfigurationBeanBuilder.java

public static JsonNode buildBeanForInput(Element element) throws JDOMException, IOException {
    ObjectNode bean = JSON_NODE_FACTORY.objectNode();
    ArrayNode inputDefinitions = bean.arrayNode();
    bean.put("inputPorts", inputDefinitions);
    ArrayNode outputDefinitions = bean.arrayNode();
    bean.put("outputPorts", outputDefinitions);

    TypeDescriptor descriptor = XMLSplitterSerialisationHelper.extensionXMLToTypeDescriptor(element);
    ObjectNode outBean = outputDefinitions.addObject();
    outBean.put("name", "output");
    outBean.put("mimeType", "'text/xml'");
    outBean.put("depth", 0);
    outBean.put("granularDepth", 0);

    if (descriptor instanceof ComplexTypeDescriptor) {
        List<TypeDescriptor> elements = ((ComplexTypeDescriptor) descriptor).getElements();
        String[] names = new String[elements.size()];
        Class<?>[] types = new Class<?>[elements.size()];
        TypeDescriptor.retrieveSignature(elements, names, types);
        for (int i = 0; i < names.length; i++) {
            ObjectNode portBean = inputDefinitions.addObject();
            portBean.put("name", names[i]);
            portBean.put("mimeType", TypeDescriptor.translateJavaType(types[i]));
            portBean.put("depth", depthForDescriptor(elements.get(i)));
        }/*from www  . j av  a  2 s.c om*/

        List<TypeDescriptor> attributes = ((ComplexTypeDescriptor) descriptor).getAttributes();
        String[] elementNames = Arrays.copyOf(names, names.length);
        Arrays.sort(elementNames);
        String[] attributeNames = new String[attributes.size()];
        Class<?>[] attributeTypes = new Class<?>[attributes.size()];
        TypeDescriptor.retrieveSignature(attributes, attributeNames, attributeTypes);
        for (int i = 0; i < attributeNames.length; i++) {
            ObjectNode portBean = inputDefinitions.addObject();
            if (Arrays.binarySearch(elementNames, attributeNames[i]) < 0) {
                portBean.put("name", attributeNames[i]);
            } else {
                portBean.put("name", "1" + attributeNames[i]);
            }
            portBean.put("mimeType", TypeDescriptor.translateJavaType(attributeTypes[i]));
            portBean.put("depth", depthForDescriptor(attributes.get(i)));
        }

    } else if (descriptor instanceof ArrayTypeDescriptor) {
        ObjectNode portBean = inputDefinitions.addObject();
        portBean.put("name", descriptor.getName());

        if (((ArrayTypeDescriptor) descriptor).getElementType() instanceof BaseTypeDescriptor) {
            portBean.put("mimeType", "l('text/plain')");
        } else {
            portBean.put("mimeType", "l('text/xml')");
        }
        portBean.put("depth", 1);
    }

    String wrappedType = new XMLOutputter().outputString(element);
    bean.put("wrappedType", wrappedType);

    return bean;
}