Example usage for org.apache.http.util ByteArrayBuffer append

List of usage examples for org.apache.http.util ByteArrayBuffer append

Introduction

In this page you can find the example usage for org.apache.http.util ByteArrayBuffer append.

Prototype

public void append(CharArrayBuffer charArrayBuffer, int i, int i2) 

Source Link

Usage

From source file:org.trancecode.http.BodypartResponseParser.java

public BodypartEntity parseBodypart(final boolean hasHeaders) throws IOException {
    if (hasHeaders) {
        parseHeaders();/*from ww  w.j a  v a  2 s .  c o m*/
    }
    if (isBinary()) {
        if (headers.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
            int length = Integer.parseInt(headers.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue());
            final byte[] in = new byte[length];
            int off = 0;
            while (length > 0) {
                final int lect = sessionBuffer.read(in, off, length);
                if (lect == -1) {
                    break;
                }
                off += lect;
                length -= lect;
            }
            sessionBuffer.readLine();
            sessionBuffer.readLine();
            return new BodypartEntity(new ByteArrayEntity(in), hasHeaders ? headers.copy() : null);
        } else {
            final ByteArrayBuffer buffer = new ByteArrayBuffer(40);
            final byte[] in = new byte[40];
            while (true) {
                final int lect = sessionBuffer.read(in, 0, 40);
                if (lect == -1) {
                    break;
                }
                buffer.append(in, 0, lect);
            }
            if (!buffer.isEmpty()) {
                sessionBuffer.readLine();
                sessionBuffer.readLine();
                return new BodypartEntity(new ByteArrayEntity(buffer.toByteArray()),
                        hasHeaders ? headers.copy() : null);
            }
        }
    } else {
        final String ch = partCharset == null ? charset : partCharset;
        final String mime = partContentType == null ? contentType : partContentType;
        final StringBuilder builder = new StringBuilder();
        boolean finish = false;
        do {
            final String line = sessionBuffer.readLine();
            if (line == null || StringUtils.startsWith(line, boundary)) {
                finish = true;
            } else {
                if (builder.length() > 0 && !isBinary()) {
                    builder.append("\n");
                }
                builder.append(line);
            }
        } while (!finish);
        if (builder.length() > 0) {
            return new BodypartEntity(new StringEntity(builder.toString(), mime, ch),
                    hasHeaders ? headers.copy() : null);
        }
    }
    return null;
}

From source file:TDS.Proctor.Services.EmbossFileService.java

/**
 * Combine one or more files into a new file with page breaks.
 *
 * @param files one or more file paths to braille files that will be combined with page breaks
 * @return the combined file contents as a byte array
 * @throws IOException//from w  w w.  jav  a2s . co m
 */
public byte[] combineFiles(String[] files) throws IOException {
    ByteArrayBuffer contents = new ByteArrayBuffer(0);

    byte[] bytes = IOUtils.toByteArray(contentRepository.findResource(files[0]));
    contents.append(bytes, 0, bytes.length);

    for (int i = 1; i < files.length; i++) {
        // page break
        contents.append(PAGE_BREAK_BYTES, 0, PAGE_BREAK_BYTES.length);

        bytes = IOUtils.toByteArray(contentRepository.findResource(files[i]));
        contents.append(bytes, 0, bytes.length);
    }

    return contents.toByteArray();
}