Cage.draw(String, OutputStream) returns before the writting to OutputStream is actually finished

59 views
Skip to first unread message

Alex Trezvy

unread,
Nov 18, 2014, 7:04:39 PM11/18/14
to cage...@googlegroups.com
Hi. I'm trying to encode the resulting image into base64 before sending it out:
        String token = cage.getTokenGenerator().next();
        HttpSession session = req.getSession(true);
        session.setAttribute(TOKEN, token);
        StringBuilder result = new StringBuilder();
        result.append("<p>Please solve the puzzle: </p>");
        result.append("<p>");
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            cage.draw(token, bytes);
            result.append("<img alt=\"CAPTCHA\" src=\"image/jpeg;base64,\"");
            result.append(Base64.encodeBase64String(bytes.toByteArray()));
            result.append("\"/>");
        } catch (IOException e) {
            result.append("ERROR HAPPENS!");
        }

        result.append("<input name=\"" + ANSWER + "\"/>");
        result.append("</p>");
        return result.toString();

What I actually get is image stuff mixed with my HTML stuff.
It seems like there are concurrent access to the 'bytes'. No good.

Alex Trezvy

unread,
Nov 19, 2014, 8:04:56 PM11/19/14
to cage...@googlegroups.com
Sorry for false alarm. My bug was due to invalid content of "src" attribute.
Reply all
Reply to author
Forward
0 new messages