Search in sources :

Example 1 with ByteArrayBuilder

use of qz.common.ByteArrayBuilder in project tray by qzind.

the class ByteUtilities method splitByteArray.

/**
 * Splits the {@code src} byte array after every {@code count}-th instance of the supplied {@code pattern} byte array.
 * <p/>
 * This is useful for large print batches that need to be split up,
 * (for example) after the P1 or ^XO command has been issued.
 * <p/>
 * TODO:
 * A rewrite of this would be a proper {@code Iteratable} interface
 * paired with an {@code Iterator} that does this automatically
 * and would eliminate the need for a {@code indicesOfMatches()} function.
 *
 * @param src     Array to split.
 * @param pattern Pattern to determine where split should occur.
 * @param count   Number of matches between splits.
 */
public static List<ByteArrayBuilder> splitByteArray(byte[] src, byte[] pattern, int count) throws NullPointerException, IndexOutOfBoundsException, ArrayStoreException {
    if (count < 1) {
        throw new IllegalArgumentException("Count cannot be less than 1");
    }
    List<ByteArrayBuilder> byteArrayList = new ArrayList<>();
    ByteArrayBuilder builder = new ByteArrayBuilder();
    Integer[] split = indicesOfMatches(src, pattern);
    int counted = 1;
    int prev = 0;
    for (int i : split) {
        // copy everything from the last pattern (or the start) to the end of this pattern
        byte[] temp = new byte[i - prev + pattern.length];
        System.arraycopy(src, prev, temp, 0, temp.length);
        builder.append(temp);
        // if we have 'count' matches, add it to list and start a new builder
        if (counted < count) {
            counted++;
        } else {
            byteArrayList.add(builder);
            builder = new ByteArrayBuilder();
            counted = 1;
        }
        prev = i + pattern.length;
    }
    // include any builder matches below 'count'
    if (!byteArrayList.contains(builder) && builder.getLength() > 0) {
        byteArrayList.add(builder);
    }
    return byteArrayList;
}
Also used : ArrayList(java.util.ArrayList) ByteArrayBuilder(qz.common.ByteArrayBuilder)

Example 2 with ByteArrayBuilder

use of qz.common.ByteArrayBuilder in project tray by qzind.

the class FileUtilities method readFile.

private static byte[] readFile(DataInputStream in) throws IOException {
    ByteArrayBuilder cmds = new ByteArrayBuilder();
    byte[] buffer = new byte[Constants.BYTE_BUFFER_SIZE];
    int len;
    while ((len = in.read(buffer)) > -1) {
        byte[] temp = new byte[len];
        System.arraycopy(buffer, 0, temp, 0, len);
        cmds.append(temp);
    }
    in.close();
    return cmds.getByteArray();
}
Also used : ByteArrayBuilder(qz.common.ByteArrayBuilder)

Example 3 with ByteArrayBuilder

use of qz.common.ByteArrayBuilder in project tray by qzind.

the class PrintRaw method print.

@Override
public void print(PrintOutput output, PrintOptions options) throws PrintException {
    PrintOptions.Raw rawOpts = options.getRawOptions();
    List<ByteArrayBuilder> pages;
    if (rawOpts.getSpoolSize() > 0 && rawOpts.getSpoolEnd() != null && !rawOpts.getSpoolEnd().isEmpty()) {
        try {
            pages = ByteUtilities.splitByteArray(commands.getByteArray(), rawOpts.getSpoolEnd().getBytes(destEncoding), rawOpts.getSpoolSize());
        } catch (UnsupportedEncodingException e) {
            throw new PrintException(e);
        }
    } else {
        pages = new ArrayList<>();
        pages.add(commands);
    }
    for (int i = 0; i < rawOpts.getCopies(); i++) {
        for (ByteArrayBuilder bab : pages) {
            try {
                if (output.isSetHost()) {
                    printToHost(output.getHost(), output.getPort(), bab.getByteArray());
                } else if (output.isSetFile()) {
                    printToFile(output.getFile(), bab.getByteArray());
                } else {
                    if (rawOpts.isForceRaw()) {
                        if (SystemUtilities.isWindows()) {
                            // Placeholder only; not yet supported
                            printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.WIN32_WMI);
                        } else {
                            // Try CUPS backend first, fallback to LPR
                            printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.CUPS_RSS, Backend.CUPS_LPR);
                        }
                    } else {
                        printToPrinter(output.getPrintService(), bab.getByteArray(), rawOpts);
                    }
                }
            } catch (IOException e) {
                throw new PrintException(e);
            }
        }
    }
}
Also used : PrintOptions(qz.printer.PrintOptions) ByteArrayBuilder(qz.common.ByteArrayBuilder) javax.print(javax.print)

Aggregations

ByteArrayBuilder (qz.common.ByteArrayBuilder)3 ArrayList (java.util.ArrayList)1 javax.print (javax.print)1 PrintOptions (qz.printer.PrintOptions)1