Example usage for java.io PushbackInputStream unread

List of usage examples for java.io PushbackInputStream unread

Introduction

In this page you can find the example usage for java.io PushbackInputStream unread.

Prototype

public void unread(byte[] b) throws IOException 

Source Link

Document

Pushes back an array of bytes by copying it to the front of the pushback buffer.

Usage

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private static void handleSetContainerAcl(HttpServletRequest request, HttpServletResponse response,
        InputStream is, BlobStore blobStore, String containerName) throws IOException, S3Exception {
    ContainerAccess access;/*from w  w  w  . j  av  a  2 s  .  co  m*/

    String cannedAcl = request.getHeader("x-amz-acl");
    if (cannedAcl == null || "private".equalsIgnoreCase(cannedAcl)) {
        access = ContainerAccess.PRIVATE;
    } else if ("public-read".equalsIgnoreCase(cannedAcl)) {
        access = ContainerAccess.PUBLIC_READ;
    } else if (CANNED_ACLS.contains(cannedAcl)) {
        throw new S3Exception(S3ErrorCode.NOT_IMPLEMENTED);
    } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    PushbackInputStream pis = new PushbackInputStream(is);
    int ch = pis.read();
    if (ch != -1) {
        pis.unread(ch);
        AccessControlPolicy policy = new XmlMapper().readValue(pis, AccessControlPolicy.class);
        String accessString = mapXmlAclsToCannedPolicy(policy);
        if (accessString.equals("private")) {
            access = ContainerAccess.PRIVATE;
        } else if (accessString.equals("public-read")) {
            access = ContainerAccess.PUBLIC_READ;
        } else {
            throw new S3Exception(S3ErrorCode.NOT_IMPLEMENTED);
        }
    }

    blobStore.setContainerAccess(containerName, access);
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private static void handleSetBlobAcl(HttpServletRequest request, HttpServletResponse response, InputStream is,
        BlobStore blobStore, String containerName, String blobName) throws IOException, S3Exception {
    BlobAccess access;//w w w.  j  a  va 2 s . c o m

    String cannedAcl = request.getHeader("x-amz-acl");
    if (cannedAcl == null || "private".equalsIgnoreCase(cannedAcl)) {
        access = BlobAccess.PRIVATE;
    } else if ("public-read".equalsIgnoreCase(cannedAcl)) {
        access = BlobAccess.PUBLIC_READ;
    } else if (CANNED_ACLS.contains(cannedAcl)) {
        throw new S3Exception(S3ErrorCode.NOT_IMPLEMENTED);
    } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    PushbackInputStream pis = new PushbackInputStream(is);
    int ch = pis.read();
    if (ch != -1) {
        pis.unread(ch);
        AccessControlPolicy policy = new XmlMapper().readValue(pis, AccessControlPolicy.class);
        String accessString = mapXmlAclsToCannedPolicy(policy);
        if (accessString.equals("private")) {
            access = BlobAccess.PRIVATE;
        } else if (accessString.equals("public-read")) {
            access = BlobAccess.PUBLIC_READ;
        } else {
            throw new S3Exception(S3ErrorCode.NOT_IMPLEMENTED);
        }
    }

    blobStore.setBlobAccess(containerName, blobName, access);
}

From source file:org.mule.transport.stdio.StdioMessageReceiver.java

@Override
public void poll() {
    String encoding = endpoint.getEncoding();
    try {//from w w w  .j av  a  2s.  co  m
        if (sendStream) {
            PushbackInputStream in = new PushbackInputStream(inputStream);

            //Block until we have some data
            int i = in.read();
            //Roll back our read
            in.unread(i);
            MuleMessage message = createMuleMessage(in, encoding);
            routeMessage(message);
        } else {
            byte[] inputBuffer = new byte[bufferSize];
            int len = inputStream.read(inputBuffer);

            if (len == -1) {
                return;
            }

            StringBuffer fullBuffer = new StringBuffer(bufferSize);
            while (len > 0) {
                fullBuffer.append(new String(inputBuffer, 0, len));
                len = 0; // mark as read
                if (inputStream.available() > 0) {
                    len = inputStream.read(inputBuffer);
                }
            }

            // Each line is a separate message
            String[] lines = fullBuffer.toString().split(SystemUtils.LINE_SEPARATOR);
            for (int i = 0; i < lines.length; ++i) {
                MuleMessage message = createMuleMessage(lines[i], encoding);
                routeMessage(message);
            }
        }

        doConnect();
    } catch (Exception e) {
        getConnector().getMuleContext().getExceptionListener().handleException(e);
    }
}

From source file:org.openhealthtools.openatna.syslog.bsd.BsdMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {//  w w w  . j  ava2s .  co m
        PushbackInputStream pin = new PushbackInputStream(in, 5);
        int priority = readPriority(pin);
        int facility;
        int severity;
        byte c;
        int spaces = 4;
        int count = 0;
        boolean spaceBefore = false;
        ByteBuffer buff = ByteBuffer.wrap(new byte[256]);

        String timestamp;
        String month = null;
        String date = null;
        String time = null;
        String host = "";
        int max = 256;
        int curr = 0;

        while (count < spaces && curr < max) {
            c = (byte) pin.read();
            curr++;
            if (c == ' ') {
                if (!spaceBefore) {
                    count++;
                    String currHeader = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
                    buff.clear();
                    switch (count) {
                    case 1:
                        month = currHeader;
                        break;
                    case 2:
                        date = currHeader;
                        break;
                    case 3:
                        time = currHeader;
                        break;
                    case 4:
                        host = currHeader;
                        break;

                    }
                }
                spaceBefore = true;

            } else {
                spaceBefore = false;
                buff.put(c);
            }
        }
        if (month == null || date == null || time == null) {
            timestamp = createDate(new Date());
        } else {
            String gap = " ";
            if (date.length() == 1) {
                gap = "  ";
            }
            timestamp = (month + gap + date + " " + time);
            try {
                formatDate(timestamp);
            } catch (Exception e) {
                timestamp = createDate(new Date());
            }
        }
        String tag = null;
        int tagLen = 32;
        buff.clear();
        for (int i = 0; i < tagLen; i++) {
            c = (byte) pin.read();
            curr++;
            if (!Character.isLetterOrDigit((char) (c & 0xff))) {
                pin.unread(c);
                break;
            }
            buff.put(c);
        }
        if (buff.position() > 0) {
            tag = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
        }

        LogMessage logMessage = getLogMessage(tag);
        String encoding = readBom(pin, logMessage.getExpectedEncoding());
        logMessage.read(pin, encoding);
        facility = priority / 8;
        severity = priority % 8;
        return new BsdMessage(facility, severity, timestamp, host, logMessage, tag);
    } catch (IOException e) {
        e.printStackTrace();
        throw new SyslogException(e);
    }
}

From source file:org.openhealthtools.openatna.syslog.GenericMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {/*w  w  w.j a v  a  2  s.  c om*/
        PushbackInputStream pin = new PushbackInputStream(in, 7);
        byte[] bytes = new byte[7];
        pin.read(bytes);

        boolean bsd = isBSD(bytes);
        pin.unread(bytes);
        if (bsd) {
            log.debug("message is BSD style");
            return bsdFactory.read(pin);
        } else {
            log.debug("message RFC 5424");
            return protFactory.read(pin);
        }
    } catch (IOException e) {
        throw new SyslogException(e);
    }
}

From source file:org.openhealthtools.openatna.syslog.protocol.ProtocolMessageFactory.java

/**
 * This reads up to 256 characters to read headers (excluding SDs). This limit is arbitrary.
 * It is imposed to reduce the risk/*from   w w  w.  j  a  va 2 s.  co  m*/
 * of badly formed or malicious messages from using too many resources.
 *
 * @param in
 * @return
 * @throws SyslogException
 */
public SyslogMessage read(InputStream in) throws SyslogException {
    try {
        PushbackInputStream pin = new PushbackInputStream(in, 5);
        int priority = readPriority(pin);
        int facility;
        int severity;
        byte c;
        int spaces = 5;
        int count = 0;
        ByteBuffer buff = ByteBuffer.wrap(new byte[256]);

        String timestamp = null;
        String host = "-";
        String app = "-";
        String proc = "-";
        String mid = "-";
        int max = 256;
        int curr = 0;

        while (count < spaces && curr < max) {
            c = (byte) pin.read();
            curr++;
            if (c == ' ') {
                count++;
                String currHeader = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
                buff.clear();
                switch (count) {
                case 1:
                    timestamp = currHeader;
                    break;
                case 2:
                    host = currHeader;
                    break;
                case 3:
                    app = currHeader;
                    break;
                case 4:
                    proc = currHeader;
                    break;
                case 5:
                    mid = currHeader;
                    break;
                }
            } else {
                buff.put(c);
            }
        }
        if (timestamp == null) {
            throw new SyslogException("no timestamp defined");
        }

        c = (byte) pin.read();
        List<StructuredElement> els = new ArrayList<StructuredElement>();
        if (c == '-') {
            c = (byte) pin.read();
            if (c != ' ') {
                throw new SyslogException("not a space");
            }
        } else if (c == '[') {
            pin.unread(c);
            els = StructuredElement.parse(pin);
        } else {
            throw new SyslogException("Illegal Structured data");
        }
        LogMessage logMessage = getLogMessage(mid);
        String encoding = readBom(pin, logMessage.getExpectedEncoding());
        logMessage.read(pin, encoding);
        facility = priority / 8;
        severity = priority % 8;
        ProtocolMessage sm = new ProtocolMessage(facility, severity, timestamp, host, logMessage, app, mid,
                proc);
        for (StructuredElement el : els) {
            sm.addStructuredElement(el);
        }
        return sm;
    } catch (IOException e) {
        e.printStackTrace();
        throw new SyslogException(e);
    }
}

From source file:org.openymsg.network.HTTPConnectionHandler.java

private String readLine(PushbackInputStream pbis) throws IOException {
    int c = pbis.read();
    StringBuffer sb = new StringBuffer();
    while (c != '\n' && c != '\r') {
        sb.append((char) c);
        c = pbis.read();/*from w w w. j  ava2  s  . c o m*/
    }
    // Check next character
    int c2 = pbis.read();
    if ((c == '\n' && c2 != '\r') || (c == '\r' && c2 != '\n'))
        pbis.unread(c2);
    return sb.toString();
}

From source file:org.osaf.cosmo.cmp.CmpServlet.java

private Document readXmlRequest(HttpServletRequest req) throws SAXException, IOException {
    if (req.getContentLength() == 0) {
        return null;
    }//from w w  w.  ja va2  s  .c  o  m
    InputStream in = req.getInputStream();
    if (in == null) {
        return null;
    }

    // check to see if there's any data to read
    PushbackInputStream filtered = new PushbackInputStream(in, 1);
    int read = filtered.read();
    if (read == -1) {
        return null;
    }
    filtered.unread(read);

    // there is data, so read the stream
    try {
        BUILDER_FACTORY.setNamespaceAware(true);
        DocumentBuilder docBuilder = BUILDER_FACTORY.newDocumentBuilder();
        return docBuilder.parse(filtered);
    } catch (ParserConfigurationException e) {
        throw new CmpException("error configuring xml builder", e);
    }
}