Example usage for java.io ByteArrayInputStream read

List of usage examples for java.io ByteArrayInputStream read

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream read.

Prototype

public synchronized int read() 

Source Link

Document

Reads the next byte of data from this input stream.

Usage

From source file:com.clt.sub.controller.DriverAction.java

/**
 * ??//from  w ww .  ja  va  2  s.c  om
 * 
 * @param request
 * @param files
 * @throws FileNotFoundException
 * @throws IOException
 */
private void uploadCardImg(HttpServletRequest request, CommonsMultipartFile[] files, TDriver driver)
        throws FileNotFoundException, IOException {
    if (files == null) {
        return;
    }
    for (int i = 0; i < files.length; i++) {
        System.out.println("fileName---------->" + files[i].getOriginalFilename());

        if (!files[i].isEmpty()) {
            int pre = (int) System.currentTimeMillis();
            // String source = request.getSession().getServletContext()
            // .getRealPath( "/" );
            String source = pictureService.getPathById(22);// ?
            if (!source.endsWith("/")) {
                source += "/";
            }
            if (StringUtils.isBlank(source)) {
                System.out.println("source??");
                return;
            }
            String path = source;
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            String jpgPath = new Date().getTime() + files[i].getOriginalFilename();
            // path += new Date().getTime() +
            // files[i].getOriginalFilename();
            path += jpgPath;
            // ?????
            FileOutputStream os = new FileOutputStream(path);
            // ?
            ByteArrayInputStream in = (ByteArrayInputStream) files[i].getInputStream();

            // ?
            int b = 0;
            while ((b = in.read()) != -1) {
                os.write(b);
            }
            os.flush();
            os.close();
            in.close();
            int finaltime = (int) System.currentTimeMillis();
            System.out.println(finaltime - pre);
            String imgPath = driver.getVcCardImgPath();// ??
            if (StringUtils.isBlank(imgPath)) {
                driver.setVcCardImgPath(jpgPath);
            } else {
                // share.setVcImgpath( imgPath + "," + path );
                imgPath = imgPath.substring(imgPath.lastIndexOf("/") + 1);// ?
                String existImgPath = source + imgPath;
                File imgFile = new File(existImgPath);
                if (imgFile.exists()) {
                    boolean isDel = imgFile.delete();
                    if (isDel) {
                        System.out.println("'" + imgPath + "'");
                    }
                }
                driver.setVcCardImgPath(jpgPath);
            }
        }
    }
}

From source file:com.clt.sub.controller.DriverAction.java

/**
 * ??/* w w  w  .j  ava 2 s  .  co  m*/
 * 
 * @param request
 * @param files
 * @throws FileNotFoundException
 * @throws IOException
 */
private void uploadDriveImg(HttpServletRequest request, CommonsMultipartFile[] files, TDriver driver)
        throws FileNotFoundException, IOException {
    if (files == null) {
        return;
    }
    for (int i = 0; i < files.length; i++) {
        System.out.println("fileName---------->" + files[i].getOriginalFilename());

        if (!files[i].isEmpty()) {
            int pre = (int) System.currentTimeMillis();
            // String source = request.getSession().getServletContext()
            // .getRealPath( "/" );
            String source = pictureService.getPathById(22);// ?
            if (!source.endsWith("/")) {
                source += "/";
            }
            if (StringUtils.isBlank(source)) {
                System.out.println("source??");
                return;
            }
            String path = source;
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            String jpgPath = new Date().getTime() + files[i].getOriginalFilename();
            // path += new Date().getTime() +
            // files[i].getOriginalFilename();
            path += jpgPath;
            // ?????
            FileOutputStream os = new FileOutputStream(path);
            // ?
            ByteArrayInputStream in = (ByteArrayInputStream) files[i].getInputStream();

            // ?
            int b = 0;
            while ((b = in.read()) != -1) {
                os.write(b);
            }
            os.flush();
            os.close();
            in.close();
            int finaltime = (int) System.currentTimeMillis();
            System.out.println(finaltime - pre);
            String imgPath = driver.getVcDriveImgPath();// ??
            if (StringUtils.isBlank(imgPath)) {
                driver.setVcDriveImgPath(jpgPath);
            } else {
                // share.setVcImgpath( imgPath + "," + path );
                imgPath = imgPath.substring(imgPath.lastIndexOf("/") + 1);// ?
                String existImgPath = source + imgPath;
                File imgFile = new File(existImgPath);
                if (imgFile.exists()) {
                    boolean isDel = imgFile.delete();
                    if (isDel) {
                        System.out.println("'" + imgPath + "'");
                    }
                }
                driver.setVcDriveImgPath(jpgPath);
            }
        }
    }
}

From source file:com.clt.sub.controller.DriverAction.java

/**
 * ?//from   w  w  w .  ja  va2s  .c  o m
 * 
 * @param request
 * @param files
 * @throws FileNotFoundException
 * @throws IOException
 */
private void uploadTruckImg(HttpServletRequest request, CommonsMultipartFile[] files, TDriver driver)
        throws FileNotFoundException, IOException {
    if (files == null) {
        return;
    }
    for (int i = 0; i < files.length; i++) {
        System.out.println("fileName---------->" + files[i].getOriginalFilename());

        if (!files[i].isEmpty()) {
            int pre = (int) System.currentTimeMillis();
            // String source = request.getSession().getServletContext()
            // .getRealPath( "/" );
            String source = pictureService.getPathById(22);// ?
            if (!source.endsWith("/")) {
                source += "/";
            }
            if (StringUtils.isBlank(source)) {
                System.out.println("source??");
                return;
            }
            String path = source;
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            String jpgPath = new Date().getTime() + files[i].getOriginalFilename();
            // path += new Date().getTime() +
            // files[i].getOriginalFilename();
            path += jpgPath;
            // ?????
            FileOutputStream os = new FileOutputStream(path);
            // ?
            ByteArrayInputStream in = (ByteArrayInputStream) files[i].getInputStream();

            // ?
            int b = 0;
            while ((b = in.read()) != -1) {
                os.write(b);
            }
            os.flush();
            os.close();
            in.close();
            int finaltime = (int) System.currentTimeMillis();
            System.out.println(finaltime - pre);
            String imgPath = driver.getVcTruckImgPath();
            if (StringUtils.isBlank(imgPath)) {
                driver.setVcTruckImgPath(jpgPath);
            } else {
                // share.setVcImgpath( imgPath + "," + path );
                imgPath = imgPath.substring(imgPath.lastIndexOf("/") + 1);// ?
                String existImgPath = source + imgPath;
                File imgFile = new File(existImgPath);
                if (imgFile.exists()) {
                    boolean isDel = imgFile.delete();
                    if (isDel) {
                        System.out.println("'" + imgPath + "'");
                    }
                }
                driver.setVcTruckImgPath(jpgPath);
            }
        }
    }
}

From source file:com.clt.sub.controller.DriverAction.java

/**
 * ??/*from  www . j  a v  a  2  s.co  m*/
 * 
 * @param request
 * @param files
 * @throws FileNotFoundException
 * @throws IOException
 */
private void uploadImg(HttpServletRequest request, CommonsMultipartFile[] files, TDriver driver)
        throws FileNotFoundException, IOException {
    if (files == null) {
        return;
    }
    for (int i = 0; i < files.length; i++) {
        System.out.println("fileName---------->" + files[i].getOriginalFilename());

        if (!files[i].isEmpty()) {
            int pre = (int) System.currentTimeMillis();
            // String source = request.getSession().getServletContext()
            // .getRealPath( "/" );
            String source = pictureService.getPathById(22);// ?
            if (!source.endsWith("/")) {
                source += "/";
            }
            if (StringUtils.isBlank(source)) {
                System.out.println("source??");
                return;
            }
            String path = source;
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            String jpgPath = new Date().getTime() + files[i].getOriginalFilename();
            // path += new Date().getTime() +
            // files[i].getOriginalFilename();
            path += jpgPath;
            // ?????
            FileOutputStream os = new FileOutputStream(path);
            // ?
            ByteArrayInputStream in = (ByteArrayInputStream) files[i].getInputStream();

            // ?
            int b = 0;
            while ((b = in.read()) != -1) {
                os.write(b);
            }
            os.flush();
            os.close();
            in.close();
            int finaltime = (int) System.currentTimeMillis();
            System.out.println(finaltime - pre);
            String imgPath = driver.getVcImgPath();
            if (StringUtils.isBlank(imgPath)) {
                driver.setVcImgPath(jpgPath);

            } else {
                // share.setVcImgpath( imgPath + "," + path );
                imgPath = imgPath.substring(imgPath.lastIndexOf("/") + 1);// ?
                String existImgPath = source + imgPath;
                File imgFile = new File(existImgPath);
                if (imgFile.exists()) {
                    boolean isDel = imgFile.delete();
                    if (isDel) {
                        System.out.println("'" + imgPath + "'");
                    }
                }
                driver.setVcImgPath(jpgPath);
            }
        }
    }
}

From source file:com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest.java

@Override
public ServletInputStream getInputStream() throws IOException {
    byte[] bodyBytes = request.getBody().getBytes();
    if (request.isBase64Encoded()) {
        bodyBytes = Base64.getDecoder().decode(request.getBody());
    }//from  w w  w. j av a  2s .  c om
    ByteArrayInputStream requestBodyStream = new ByteArrayInputStream(bodyBytes);
    return new ServletInputStream() {

        private ReadListener listener;

        @Override
        public boolean isFinished() {
            return true;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
            listener = readListener;
            try {
                listener.onDataAvailable();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int read() throws IOException {
            int readByte = requestBodyStream.read();
            if (requestBodyStream.available() == 0 && listener != null) {
                listener.onAllDataRead();
            }
            return readByte;
        }
    };
}

From source file:org.apache.click.servlet.MockRequest.java

/**
 * Returns an input stream if there has been added some uploaded files. Use
 * {@link #addFile(String, File, String)} to add some uploaded files.
 *
 * @return The input stream//  w  ww . j a  va2 s. c o m
 * @throws IOException If an I/O related problem occurs
 */
public ServletInputStream getInputStream() throws IOException {
    if (uploadedFiles != null && uploadedFiles.size() > 0) {
        byte[] request = buildRequest();

        // Ok lets make an input stream to return
        final ByteArrayInputStream bais = new ByteArrayInputStream(request);

        return new ServletInputStream() {

            @Override
            public int read() {
                return bais.read();
            }
        };
    } else {
        return new ServletInputStream() {

            @Override
            public int read() {
                return -1;
            }
        };
    }
}

From source file:org.jopenray.client.RendererListener.java

private void handlePacket() throws IOException {
    byteCount += dataLength;/*ww  w. j a  va  2 s  . co m*/
    packetCount++;

    PrintStream out = System.out;
    boolean dump = false;
    ByteArrayInputStream bIn = new ByteArrayInputStream(udpData, 0, this.dataLength);
    int r = readInt16(bIn);
    if (r == 1) {
        out.println("===================================================================================");
    }
    out.print("Seq number:" + r);
    int flag = readInt16(bIn);
    out.print(" Flag:" + flag);
    int type = readInt16(bIn);
    out.print(" Type:" + type);
    int dir = readInt16(bIn);
    out.println(" Dir:" + dir + " dataSize:" + udpData.length);
    if (dir != 2000) {

        // Server -> Sunray
        int a = readInt16(bIn);
        int b = readInt16(bIn);
        int c = readInt16(bIn);
        int d = readInt16(bIn);
        out.println("Server -> Sunray:" + a + "," + b + "," + c + "," + d);
        boolean endOfFrame = false;
        while (bIn.available() > 0 && !endOfFrame) {
            String opCodeHeader = "";
            int opcode = bIn.read();
            opCodeHeader += "[ Opcode: " + opcode;
            int f = bIn.read();
            opCodeHeader += " Flag" + f;
            int oseq = readInt16(bIn);
            opCodeHeader += " OpcodeSeq:" + oseq;
            int x = readInt16(bIn);
            int y = readInt16(bIn);

            int w = readInt16(bIn);
            int h = readInt16(bIn);

            opCodeHeader += " x,y: " + x + "," + y + " w:" + w + " h:" + h + " ]";

            switch (opcode) {
            case 0x0: {
                endOfFrame = true;
                break;
            }
            case 0x03:
                out.println("0x03 Strange opcode " + opCodeHeader);
                break;
            case 0xA1:
                int ap1 = bIn.read();
                int ap2 = bIn.read();
                int ap3 = bIn.read();
                int ap4 = bIn.read();
                out.println("0xA1:" + ap1 + "," + ap2 + "," + ap3 + "," + ap4 + opCodeHeader);
                break;
            case 0xA2:
                // out.println("FillRect");
                int a2p1 = bIn.read();
                int a2p2 = bIn.read();
                int a2p3 = bIn.read();
                int a2p4 = bIn.read();
                out.println("FillRect: Color:" + a2p1 + "," + a2p2 + "," + a2p3 + "," + a2p4 + opCodeHeader);
                break;
            case 0xA3: {

                int a3p1 = bIn.read();
                int a3p2 = bIn.read();
                int a3p3 = bIn.read();
                int a3p4 = bIn.read();
                int nbBytesPerRow = round(w, 8) / 8;
                int nbBytes = round(nbBytesPerRow * h, 4);
                byte[] unkuonw = new byte[nbBytes];
                try {
                    int lRead = bIn.read(unkuonw);
                    out.println("FillRectBitmap: Color:" + a3p1 + "," + a3p2 + "," + a3p3 + "," + a3p4
                            + " | bytes/row:" + nbBytesPerRow + "l:" + nbBytes + " lRead:" + lRead
                            + opCodeHeader);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            case 0xA4:
                int xsrc = readInt16(bIn);
                int ysrc = readInt16(bIn);
                out.println("CopyRect from : " + xsrc + "," + ysrc + opCodeHeader);

                break;
            case 0xA5: {
                // out.println("SetRectBitmap");
                // err.println("SetRectBitmap not yet implemented");
                try {
                    Color c1 = readColor(bIn);
                    Color c2 = readColor(bIn);
                    int nbBytesPerRow = round(w, 8) / 8;
                    int nbBytes = round(nbBytesPerRow * h, 4);
                    byte[] unkuonw = new byte[nbBytes];

                    int lRead = bIn.read(unkuonw);

                    out.println("SetRectBitmap: " + w + "x" + h + " at " + x + "," + y + " Color:" + c1 + " / "
                            + c2 + " | bytes/row:" + nbBytesPerRow + " l:" + nbBytes + " lRead:" + lRead
                            + opCodeHeader);
                    if (nbBytes > 1024) {
                        out.println("! data too long:" + nbBytes);
                    } else {
                        HexDump.dump(unkuonw, 0, System.err, 0);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    dump = true;
                }

                break;
            }
            case 0xA6: {

                out.println("SetRect:" + opCodeHeader);
                int nbBytesPerRow = round(w * 3, 4);
                int nbBytes = round(nbBytesPerRow * h, 4);
                // int nbBytes=w*h;
                if (nbBytes > 1000000) {
                    System.out.println("Bad length:" + nbBytes);
                } else {
                    byte[] colors = new byte[nbBytes];

                    int lRead = bIn.read(colors);
                    if (lRead != nbBytes) {
                        System.out.println("Bad length:" + nbBytes + " != " + lRead);
                    }
                    // colors contains colors (r,g,b)
                }
                break;
            }
            case 0xA8: {
                int xbound = readInt16(bIn);
                int ybound = readInt16(bIn);
                int wbound = readInt16(bIn);
                int hbound = readInt16(bIn);
                out.println("SetMouseBound to: " + xbound + "," + ybound + " w:" + wbound + " h:" + hbound + " "
                        + opCodeHeader + opCodeHeader);

                break;
            }
            case 0xA9: {

                Color c1 = readColor(bIn);
                Color c2 = readColor(bIn);
                out.println("SetMousePointer pos:" + x + "," + y + " size:" + w + "x" + h + " Color:" + c1
                        + " , " + c2 + opCodeHeader);
                int l = (w * h) / 8;
                byte[] b1 = new byte[l];
                bIn.read(b1);
                out.println("Bitmap");
                // printBits(w, h, b1);

                byte[] b2 = new byte[l];
                bIn.read(b2);
                out.println("Mask");
                // printBits(w, h, b2);
                break;
            }
            case 0xAA:
                int aap1 = bIn.read();
                int aap2 = bIn.read();
                int aap3 = bIn.read();
                int aap4 = bIn.read();
                out.println("SetMousePosition: " + aap1 + "," + aap2 + "," + aap3 + "," + aap4 + opCodeHeader);
                break;
            case 0xAB:
                int ab1 = readInt16(bIn);
                int ab2 = readInt16(bIn);
                out.println("SetKeyLock: " + ab1 + " " + ab2 + opCodeHeader);

                break;
            case 0xAC:

                int ac1 = readInt16(bIn);
                int ac2 = readInt16(bIn);
                int ac3 = readInt16(bIn);
                int ac4 = readInt16(bIn);
                out.println(
                        "0xAC : " + ac1 + " , " + ac2 + "," + ac3 + " , " + ac4 + opCodeHeader + opCodeHeader);
                break;
            case 0xAD:
                out.println("0xAD" + opCodeHeader);

                int l = readInt16(bIn);
                // l = (l & 0xfffc) + 2;

                out.println("l: " + l);
                out.println("(l & 0xfffc) + 2 :" + (l & 0xfffc) + 2);
                byte[] unkuonwn = new byte[l];
                dump = true;
                try {
                    int lRead = bIn.read(unkuonwn);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            case 0xAF: {

                int p1 = bIn.read();
                int p2 = bIn.read();
                int p3 = bIn.read();
                int p4 = bIn.read();
                for (int i = 0; i < 8; i++) {
                    bIn.read();
                }
                if (p1 != 255 && p2 != 255 && p3 != 255 && p4 != 255) {
                    out.println("PAD:" + p1 + "," + p2 + "," + p3 + "," + p4 + opCodeHeader);
                } else {
                    out.println("PAD " + opCodeHeader);
                }
                break;
            }
            case 0xB1:

                out.println("AUDIO:" + r + "|" + flag + "|" + type + "|" + dir + " l:" + udpData.length + " "
                        + opCodeHeader);
                // outTest.print("AUDIO:"+r+"|"+flag+"|"+type+"|"+dir+" " +
                // opCodeHeader);
                /*
                 * int xbound = readInt16(bIn); int ybound = readInt16(bIn);
                 * int wbound = readInt16(bIn); int hbound = readInt16(bIn);
                 * out.println(" to: " + xbound + "," + ybound + " w:" +
                 * wbound + " h:" + hbound + " " + opCodeHeader +
                 * opCodeHeader); dump=true;
                 */

                int v1 = 0;
                int v2 = 0;
                int totalv1et2 = 0;
                int bigTotal = 0;
                while (bIn.available() >= 0) {
                    int b1 = bIn.read();
                    int b2 = bIn.read();
                    if (b1 == -1 && b2 == -1) {
                        // outTest.print(totalv1et2+" : big total: "+bigTotal);
                        break;
                    }
                    // soundOut.write(b2);
                    // soundOut.write(b1);

                    if (b1 == 0x7F && b2 == 0xFF) {
                        v1++;
                        bigTotal++;
                        totalv1et2++;
                        if (v2 > 0)
                            out.println("v2=" + v2);
                        v2 = 0;
                    } else if (b1 == 0x80 && b2 == 0x01) {
                        v2++;
                        totalv1et2++;
                        bigTotal++;
                        if (v1 > 0)
                            out.println("v1=" + v1);
                        v1 = 0;
                    } else {
                        if (v2 > 0)
                            out.println("v2=" + v2);
                        if (v1 > 0)
                            out.println("v1=" + v1);
                        out.println("Unknwon:" + b1 + " et " + b2 + "[" + (b1 * 256 + b2) + "] total v1+v2:"
                                + totalv1et2);
                        // if(totalv1et2>0)
                        // outTest.print(totalv1et2+",");
                        v1 = 0;
                        v2 = 0;
                        totalv1et2 = 0;
                    }
                    /*
                     * bIn.read(); bIn.read(); for (int j = 0; j < 8; j++) {
                     * for (int i = 0; i < 12; i++) {
                     * 
                     * int aaa1 = bIn.read(); int aaa2 = bIn.read(); if (i %
                     * 2 == 0) { soundOut.write(aaa2); soundOut.write(aaa1);
                     * } } }
                     */

                }
                // outTest.println();

                break;
            case 0xD1:
                out.println("0xD1 " + opCodeHeader + opCodeHeader);
                break;
            case 0xD8:
                out.println("0xD8 " + opCodeHeader + opCodeHeader);
                break;
            case 0xB0: {
                out.println("0xB0 " + opCodeHeader + opCodeHeader);
                int p1 = readInt16(bIn);

                int p2 = readInt16(bIn);
                int p3 = readInt16(bIn);
                int nb = readInt16(bIn);
                out.println(p1 + " ; " + p2 + " ; " + p3);
                for (int i = 0; i < nb; i++) {
                    int xx = readInt16(bIn);
                    int yy = readInt16(bIn);
                    int ww = readInt16(bIn);
                    int hh = readInt16(bIn);
                    out.println("[" + xx + "," + yy + " " + ww + "x" + hh + "]");
                }
                break;
            }
            case 0xB4: {
                // ??
                out.println("0xB4 " + opCodeHeader + opCodeHeader);

                for (int i = 0; i < 19; i++) {
                    int p1 = readInt16(bIn);
                    out.print(p1 + ",");
                }
                int end = readInt16(bIn);
                out.println(end);
                break;
            }
            case 0xB9: {
                // ??
                out.println("0xB9 " + opCodeHeader + opCodeHeader);
                break;
            }
            case 0xBF: {
                int le = readInt16(bIn);
                out.println("0xBF " + le + " bytes " + opCodeHeader);

                byte[] unknown = new byte[le];

                try {
                    int lRead = bIn.read(unknown);
                    if (lRead != le) {
                        out.println("Bad length:" + lRead + " / " + le);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            default:
                out.println("Unknown opcode:" + opcode + opCodeHeader);
                dump = true;
                endOfFrame = true;
                break;
            }
        }

    } else {
        out.println("Unknown packet direction:" + dir);
        if (dir != 0)
            dump = true;
    }
    if (dump) {
        HexDump.dump(udpData, 0, System.err, 0);
    }
}

From source file:net.sf.jasperreports.engine.export.JRRtfExporter.java

/**
 * Export a image object//from w ww  .  j  av a  2  s .co  m
 * @param printImage JasperReports image object (JRPrintImage)
 * @throws JRException
 * @throws IOException
 */
public void exportImage(JRPrintImage printImage) throws JRException, IOException {
    int leftPadding = printImage.getLineBox().getLeftPadding();
    int topPadding = printImage.getLineBox().getTopPadding();
    int rightPadding = printImage.getLineBox().getRightPadding();
    int bottomPadding = printImage.getLineBox().getBottomPadding();

    int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
    availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

    int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
    availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight;

    Renderable renderer = printImage.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        InternalImageProcessor imageProcessor = new InternalImageProcessor(printImage,
                printImage.getScaleImageValue() != ScaleImageEnum.FILL_FRAME, availableImageWidth,
                availableImageHeight);

        InternalImageProcessorResult imageProcessorResult = null;

        try {
            imageProcessorResult = imageProcessor.process(renderer);
        } catch (Exception e) {
            Renderable onErrorRenderer = getRendererUtil().handleImageError(e,
                    printImage.getOnErrorTypeValue());
            if (onErrorRenderer != null) {
                imageProcessorResult = imageProcessor.process(onErrorRenderer);
            }
        }

        if (imageProcessorResult != null)//FIXMERTF draw image background for null images, like the other exporters do
        {
            int imageWidth = 0;
            int imageHeight = 0;
            int xoffset = 0;
            int yoffset = 0;
            int cropTop = 0;
            int cropLeft = 0;
            int cropBottom = 0;
            int cropRight = 0;

            switch (printImage.getScaleImageValue()) {
            case CLIP: {
                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = imageProcessorResult.dimension;
                if (dimension != null) {
                    normalWidth = (int) dimension.getWidth();
                    normalHeight = (int) dimension.getHeight();
                }

                switch (printImage.getHorizontalImageAlign()) {
                case RIGHT: {
                    cropLeft = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth;
                    cropRight = 0;
                    break;
                }
                case CENTER: {
                    cropLeft = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth / 2;
                    cropRight = cropLeft;
                    break;
                }
                case LEFT:
                default: {
                    cropLeft = 0;
                    cropRight = 65536 * (-availableImageWidth + normalWidth) / availableImageWidth;
                    break;
                }
                }
                switch (printImage.getVerticalImageAlign()) {
                case TOP: {
                    cropTop = 0;
                    cropBottom = 65536 * (-availableImageHeight + normalHeight) / normalHeight;
                    break;
                }
                case MIDDLE: {
                    cropTop = 65536 * (-availableImageHeight + normalHeight) / normalHeight / 2;
                    cropBottom = cropTop;
                    break;
                }
                case BOTTOM:
                default: {
                    cropTop = 65536 * (-availableImageHeight + normalHeight) / normalHeight;
                    cropBottom = 0;
                    break;
                }
                }
                imageWidth = availableImageWidth;
                imageHeight = availableImageHeight;
                break;
            }
            case FILL_FRAME: {
                imageWidth = availableImageWidth;
                imageHeight = availableImageHeight;
                break;
            }
            case RETAIN_SHAPE:
            default: {
                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = imageProcessorResult.dimension;
                if (dimension != null) {
                    normalWidth = (int) dimension.getWidth();
                    normalHeight = (int) dimension.getHeight();
                }

                double ratio = (double) normalWidth / (double) normalHeight;

                if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                    normalWidth = availableImageWidth;
                    normalHeight = (int) (availableImageWidth / ratio);
                } else {
                    normalWidth = (int) (availableImageHeight * ratio);
                    normalHeight = availableImageHeight;
                }

                xoffset = (int) (ImageUtil.getXAlignFactor(printImage) * (availableImageWidth - normalWidth));
                yoffset = (int) (ImageUtil.getYAlignFactor(printImage) * (availableImageHeight - normalHeight));
                imageWidth = normalWidth;
                imageHeight = normalHeight;

                break;
            }
            }

            startElement(printImage);
            exportPen(printImage.getForecolor());//FIXMEBORDER should we have lineColor here, if at all needed?
            finishElement();
            boolean startedHyperlink = exportHyperlink(printImage);

            contentWriter
                    .write("{\\shp{\\*\\shpinst\\shpbxpage\\shpbypage\\shpwr5\\shpfhdr0\\shpfblwtxt0\\shpz");
            contentWriter.write(String.valueOf(zorder++));
            contentWriter.write("\\shpleft");
            contentWriter.write(
                    String.valueOf(LengthUtil.twip(printImage.getX() + leftPadding + xoffset + getOffsetX())));
            contentWriter.write("\\shpright");
            contentWriter.write(String.valueOf(
                    LengthUtil.twip(printImage.getX() + leftPadding + xoffset + getOffsetX() + imageWidth)));
            contentWriter.write("\\shptop");
            contentWriter.write(
                    String.valueOf(LengthUtil.twip(printImage.getY() + topPadding + yoffset + getOffsetY())));
            contentWriter.write("\\shpbottom");
            contentWriter.write(String.valueOf(
                    LengthUtil.twip(printImage.getY() + topPadding + yoffset + getOffsetY() + imageHeight)));
            contentWriter.write("{\\sp{\\sn shapeType}{\\sv 75}}");
            contentWriter.write("{\\sp{\\sn fFilled}{\\sv 0}}");
            contentWriter.write("{\\sp{\\sn fLockAspectRatio}{\\sv 0}}");

            contentWriter.write("{\\sp{\\sn cropFromTop}{\\sv ");
            contentWriter.write(String.valueOf(cropTop));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromLeft}{\\sv ");
            contentWriter.write(String.valueOf(cropLeft));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromBottom}{\\sv ");
            contentWriter.write(String.valueOf(cropBottom));
            contentWriter.write("}}");
            contentWriter.write("{\\sp{\\sn cropFromRight}{\\sv ");
            contentWriter.write(String.valueOf(cropRight));
            contentWriter.write("}}");

            writeShapeHyperlink(printImage);

            if (printImage.getAnchorName() != null) {
                writeAnchor(printImage.getAnchorName());
            }

            contentWriter.write("{\\sp{\\sn pib}{\\sv {\\pict");
            if (imageProcessorResult.imageType == ImageTypeEnum.JPEG) {
                contentWriter.write("\\jpegblip");
            } else {
                contentWriter.write("\\pngblip");
            }
            contentWriter.write("\n");

            ByteArrayInputStream bais = new ByteArrayInputStream(imageProcessorResult.imageData);

            int count = 0;
            int current = 0;
            while ((current = bais.read()) != -1) {
                String helperStr = Integer.toHexString(current);
                if (helperStr.length() < 2) {
                    helperStr = "0" + helperStr;
                }
                contentWriter.write(helperStr);
                count++;
                if (count == 64) {
                    contentWriter.write("\n");
                    count = 0;
                }
            }

            contentWriter.write("\n}}}");
            contentWriter.write("}}\n");
            endHyperlink(startedHyperlink);
        }
    }

    int x = printImage.getX() + getOffsetX();
    int y = printImage.getY() + getOffsetY();
    int width = printImage.getWidth();
    int height = printImage.getHeight();

    if (printImage.getLineBox().getTopPen().getLineWidth() <= 0f
            && printImage.getLineBox().getLeftPen().getLineWidth() <= 0f
            && printImage.getLineBox().getBottomPen().getLineWidth() <= 0f
            && printImage.getLineBox().getRightPen().getLineWidth() <= 0f) {
        if (printImage.getLinePen().getLineWidth() > 0f) {
            exportPen(printImage.getLinePen(), x, y, width, height);
        }
    } else {
        exportBox(printImage.getLineBox(), x, y, width, height);
    }
}

From source file:org.apache.wicket.protocol.http.mock.MockHttpServletRequest.java

/**
 * Returns an input stream if there has been added some uploaded files. Use
 * {@link #addFile(String, File, String)} to add some uploaded files.
 * //from w  w  w.ja  va  2  s. c o m
 * @return The input stream
 * @throws IOException
 *             If an I/O related problem occurs
 */
@Override
public ServletInputStream getInputStream() throws IOException {
    byte[] request = buildRequest();

    // Ok lets make an input stream to return
    final ByteArrayInputStream bais = new ByteArrayInputStream(request);

    return new ServletInputStream() {
        private boolean isFinished = false;
        private boolean isReady = true;

        @Override
        public boolean isFinished() {
            return isFinished;
        }

        @Override
        public boolean isReady() {
            return isReady;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }

        @Override
        public int read() {
            isFinished = true;
            isReady = false;
            return bais.read();
        }
    };
}

From source file:com.flexoodb.common.FlexUtils.java

static public void saveBytesToFile(String filepath, byte[] data) throws Exception {
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    java.io.FileOutputStream fos = new java.io.FileOutputStream(new File(filepath));
    while (is.available() > 0) {
        fos.write(is.read());
    }/*  w w  w  . j  a  v a  2 s . com*/
    fos.close();
    is.close();
}