Example usage for io.vertx.core.buffer Buffer appendBytes

List of usage examples for io.vertx.core.buffer Buffer appendBytes

Introduction

In this page you can find the example usage for io.vertx.core.buffer Buffer appendBytes.

Prototype

@GenIgnore(GenIgnore.PERMITTED_TYPE)
@Fluent
Buffer appendBytes(byte[] bytes, int offset, int len);

Source Link

Document

Appends the specified number of bytes from byte[] to the end of the Buffer, starting at the given offset.

Usage

From source file:com.sibvisions.vertx.HttpServer.java

License:Apache License

/**
 * Handles a download request./*from  w  w w.  j a  v a 2s.c  o  m*/
 * 
 * @param pRequest the request
 */
private void handleDownload(HttpServerRequest pRequest) {
    String sKey = pRequest.params().get("KEY");

    if (sKey == null) {
        pRequest.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code());
        pRequest.response().end();

        return;
    }

    IFileHandle fh = (IFileHandle) ObjectCache.get(sKey);

    HttpServerResponse response = pRequest.response();

    String sType = MimeMapping.getMimeTypeForExtension(FileUtil.getExtension(fh.getFileName()));

    if (sType != null) {
        response.putHeader(HttpHeaders.CONTENT_TYPE, sType);
    }

    response.putHeader("Content-Disposition", "attachment; filename=\"" + fh.getFileName() + "\"");

    int iLen;

    byte[] byContent = new byte[4096];

    try {
        response.putHeader(HttpHeaders.CONTENT_LENGTH, "" + fh.getLength());

        InputStream in = fh.getInputStream();

        Buffer buffer;

        while ((iLen = in.read(byContent)) >= 0) {
            buffer = Buffer.buffer();
            buffer.appendBytes(byContent, 0, iLen);

            response.write(buffer);
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    response.end();
}

From source file:org.eclipse.hono.deviceregistry.DeviceRegistryTestUtils.java

License:Open Source License

/**
 * Reads the contents from a file using this class' class loader.
 * //  w w w.  j a va  2s  .co  m
 * @param resourceName The name of the resource to load.
 * @return The contents of the file.
 * @throws IOException if the file cannot be read.
 */
public static Buffer readFile(final String resourceName) throws IOException {

    final Buffer result = Buffer.buffer();
    try (InputStream is = DeviceRegistryTestUtils.class.getResourceAsStream(resourceName)) {
        int bytesRead = 0;
        final byte[] readBuffer = new byte[4096];
        while ((bytesRead = is.read(readBuffer)) != -1) {
            result.appendBytes(readBuffer, 0, bytesRead);
        }
    }
    return result;
}

From source file:org.sfs.io.BufferEndableWriteStreamOutputStream.java

License:Apache License

@Override
public void write(byte[] b, int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return;//from   w w  w.  j  av a  2s . c o  m
    }
    Buffer buffer = buffer(len);
    delegate.write(buffer.appendBytes(b, off, len));
}

From source file:org.sfs.io.ReadStreamDataHandlerOutputStream.java

License:Apache License

@Override
public void write(byte[] b, int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return;/*from w ww .j a  v  a2 s. c o m*/
    }
    Buffer buffer = buffer(len);
    dataHandler.handle(buffer.appendBytes(b, off, len));
}