Example usage for java.io PushbackInputStream PushbackInputStream

List of usage examples for java.io PushbackInputStream PushbackInputStream

Introduction

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

Prototype

public PushbackInputStream(InputStream in) 

Source Link

Document

Creates a PushbackInputStream with a 1-byte pushback buffer, and saves its argument, the input stream in, for later use.

Usage

From source file:com.ning.metrics.serialization.smile.SmileEnvelopeEventDeserializer.java

/**
 * Extracts all events in the stream/*from www . java 2s.c  o  m*/
 * Note: Stream must be formatted as an array of (serialized) SmileEnvelopeEvents.
 *
 * @param in InputStream containing events
 * @return A list of SmileEnvelopeEvents
 * @throws IOException generic I/O exception
 */
public static List<SmileEnvelopeEvent> extractEvents(final InputStream in) throws IOException {
    final PushbackInputStream pbIn = new PushbackInputStream(in);

    final byte firstByte = (byte) pbIn.read();

    // EOF?
    if (firstByte == -1) {
        return null;
    }

    pbIn.unread(firstByte);

    SmileEnvelopeEventDeserializer deser = (firstByte == SMILE_MARKER)
            ? new SmileEnvelopeEventDeserializer(pbIn, false)
            : new SmileEnvelopeEventDeserializer(pbIn, true);
    final List<SmileEnvelopeEvent> events = new LinkedList<SmileEnvelopeEvent>();

    while (deser.hasNextEvent()) {
        SmileEnvelopeEvent event = deser.getNextEvent();
        if (event == null) {
            // TODO: this is NOT expected, should warn
            break;
        }
        events.add(event);
    }
    return events;
}

From source file:org.cruk.genologics.api.GenologicsAPIBatchOperationTest.java

@Test
public void testArtifactBatchFetch() throws Exception {
    List<LimsLink<Artifact>> links = new ArrayList<LimsLink<Artifact>>();

    links.add(new ArtifactLink(new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/2-1000624")));
    links.add(new ArtifactLink(new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/2-1000622")));
    links.add(new ArtifactLink(new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/2-1000605")));
    links.add(new ArtifactLink(new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/2-1000623")));
    links.add(new ArtifactLink(new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/2-1000625")));

    File expectedResultFile = new File("src/test/xml/batchtestreordering-artifacts.xml");
    String expectedReply = FileUtils.readFileToString(expectedResultFile);

    URI uri = new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/batch/retrieve");

    // Note: need PushbackInputStream to prevent the call to getBody() being made more than once.
    // See MessageBodyClientHttpResponseWrapper.

    InputStream responseStream = new PushbackInputStream(new ByteArrayInputStream(expectedReply.getBytes()));

    HttpHeaders headers = new HttpHeaders();

    ClientHttpResponse httpResponse = EasyMock.createMock(ClientHttpResponse.class);
    EasyMock.expect(httpResponse.getStatusCode()).andReturn(HttpStatus.OK).anyTimes();
    EasyMock.expect(httpResponse.getRawStatusCode()).andReturn(HttpStatus.OK.value()).anyTimes();
    EasyMock.expect(httpResponse.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpResponse.getBody()).andReturn(responseStream).once();
    httpResponse.close();//from w w  w.j  av  a 2 s. co m
    EasyMock.expectLastCall().once();

    ClientHttpRequest httpRequest = EasyMock.createMock(ClientHttpRequest.class);
    EasyMock.expect(httpRequest.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpRequest.getBody()).andReturn(new NullOutputStream()).times(0, 2);
    EasyMock.expect(httpRequest.execute()).andReturn(httpResponse).once();

    ClientHttpRequestFactory mockFactory = EasyMock.createStrictMock(ClientHttpRequestFactory.class);
    EasyMock.expect(mockFactory.createRequest(uri, HttpMethod.POST)).andReturn(httpRequest).once();

    restTemplate.setRequestFactory(mockFactory);

    EasyMock.replay(httpResponse, httpRequest, mockFactory);

    List<Artifact> artifacts = api.loadAll(links);

    assertEquals("Wrong number of artifacts", links.size(), artifacts.size());

    for (int i = 0; i < links.size(); i++) {
        assertTrue("Artifact " + i + " wrong: " + artifacts.get(i).getUri(),
                artifacts.get(i).getUri().toString().startsWith(links.get(i).getUri().toString()));
    }

    EasyMock.verify(httpResponse, httpRequest, mockFactory);
}

From source file:libepg.ts.reader.Reader2.java

/**
 * ??????<br>//from  w w w .java2  s  . c  om
 * 1:???????????1??????<br>
 * 2:?????????????<br>
 * 3:??????1??????<br>
 * ???????????<br>
 * 4:1?<br>
 *
 * @return ???
 */
public synchronized List<TsPacket> getPackets() {
    ByteBuffer packetBuffer = ByteBuffer.allocate(TsPacket.TS_PACKET_BYTE_LENGTH.PACKET_LENGTH.getByteLength());
    byte[] byteData = new byte[1];

    //?
    List<TsPacket> packets = new ArrayList<>();

    FileInputStream fis = null;
    PushbackInputStream pis = null;
    try {

        fis = new FileInputStream(this.TSFile);
        pis = new PushbackInputStream(fis);

        boolean tipOfPacket = false;//?

        long count = 0;

        //??????1??????
        while (pis.read(byteData) != EOF) {

            //???????????????
            if ((byteData[0] == TsPacket.TS_SYNC_BYTE) && (tipOfPacket == false)) {
                tipOfPacket = true;
                if (LOG.isTraceEnabled() && NOT_DETERRENT_READ_TRACE_LOG) {
                    LOG.trace(
                            "???????????1????");
                }
                pis.unread(byteData);
            }

            if (tipOfPacket == true) {
                byte[] tsPacketData = new byte[TsPacket.TS_PACKET_BYTE_LENGTH.PACKET_LENGTH.getByteLength()];
                if (pis.read(tsPacketData) != EOF) {
                    if (LOG.isTraceEnabled() && NOT_DETERRENT_READ_TRACE_LOG) {
                        LOG.trace(
                                "??????????????");
                    }
                    packetBuffer.put(tsPacketData);
                } else {
                    break;
                }
            }

            if (packetBuffer.remaining() == 0) {
                byte[] BeforeCutDown = packetBuffer.array();
                byte[] AfterCutDown = new byte[packetBuffer.position()];
                System.arraycopy(BeforeCutDown, 0, AfterCutDown, 0, AfterCutDown.length);

                //??????????
                TsPacket tsp = new TsPacket(AfterCutDown);

                //                        LOG.debug(Hex.encodeHexString(tsp.getData()));
                if (LOG.isTraceEnabled() && NOT_DETERRENT_READ_TRACE_LOG) {
                    LOG.trace(
                            "1???????? ");
                    LOG.trace(tsp.toString());
                }

                if (tsp.getTransport_error_indicator() != 0) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn(
                                "??1????????????????????");
                        LOG.warn(tsp);
                        LOG.warn(TSFile);
                    }
                    tipOfPacket = false;
                } else {
                    packets.add(tsp);
                    count++;
                }
                packetBuffer.clear();
                tipOfPacket = false;

                if (this.readLimit != null && count >= this.readLimit) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info(
                                "????????????? ?? = "
                                        + this.readLimit);
                    }
                    break;
                }
            }

        }
        if (LOG.isTraceEnabled() && NOT_DETERRENT_READ_TRACE_LOG) {
            LOG.trace("?????????");
            LOG.trace(" = " + Hex.encodeHexString(packetBuffer.array()));
        }

        pis.close();
        fis.close();
        LOG.info("??? = " + count);

    } catch (FileNotFoundException e) {
        LOG.fatal("?????", e);
    } catch (IOException e) {
        LOG.fatal("???", e);
    }
    return Collections.unmodifiableList(packets);
}

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

/**
 * The only time Yahoo can actually send us any packets is when we send it some. Yahoo encodes its packets in a POST
 * body - the format is the same binary representation used for direct connections (with one or two extra codes).
 * /*from   www . j  a v  a2 s  .  co m*/
 * After posting a packet, the connection will receive a HTTP response who's payload consists of four bytes followed
 * by zero or more packets. The first byte of the four is a count of packets encoded in the following body.
 * 
 * Each incoming packet is transfered to a queue, where receivePacket() takes them off - thus preserving the effect
 * that input and output packets are being received independently, as with other connection handlers. As
 * readPacket() can throw an exception, these are caught and transfered onto the queue too, then rethrown by
 * receivePacket() .
 */
@Override
synchronized void sendPacket(PacketBodyBuffer body, ServiceType service, long status, long sessionID)
        throws IOException, IllegalStateException {
    if (!connected)
        throw new IllegalStateException("Not logged in");

    if (filterOutput(body, service))
        return;
    byte[] b = body.getBuffer();

    Socket soc = new Socket(proxyHost, proxyPort);
    PushbackInputStream pbis = new PushbackInputStream(soc.getInputStream());
    DataOutputStream dos = new DataOutputStream(soc.getOutputStream());

    // HTTP header
    dos.writeBytes(HTTP_HEADER_POST);
    dos.writeBytes("Content-length: " + (b.length + NetworkConstants.YMSG9_HEADER_SIZE) + NetworkConstants.END);
    dos.writeBytes(HTTP_HEADER_AGENT);
    dos.writeBytes(HTTP_HEADER_HOST);
    if (HTTP_HEADER_PROXY_AUTH != null)
        dos.writeBytes(HTTP_HEADER_PROXY_AUTH);
    if (cookie != null)
        dos.writeBytes("Cookie: " + cookie + NetworkConstants.END);
    dos.writeBytes(NetworkConstants.END);
    // YMSG9 header
    dos.write(NetworkConstants.MAGIC, 0, 4);
    dos.write(NetworkConstants.VERSION_HTTP, 0, 4);
    dos.writeShort(b.length & 0xffff);
    dos.writeShort(service.getValue() & 0xffff);
    dos.writeInt((int) (status & 0xffffffff));
    dos.writeInt((int) (sessionID & 0xffffffff));
    // YMSG9 body
    dos.write(b, 0, b.length);
    dos.flush();

    // HTTP response header
    String s = readLine(pbis);
    if (s == null || s.indexOf(" 200 ") < 0) // Not "HTTP/1.0 200 OK"
    {
        throw new IOException("HTTP request returned didn't return OK (200): " + s);
    }
    while (s != null && s.trim().length() > 0)
        // Read past header
        s = readLine(pbis);
    // Payload count
    byte[] code = new byte[4];
    int res = pbis.read(code, 0, 4); // Packet count (Little-Endian?)
    if (res < 4) {
        throw new IOException("Premature end of HTTP data");
    }
    int count = code[0];
    // Payload body
    YMSG9InputStream yip = new YMSG9InputStream(pbis);
    YMSG9Packet pkt;
    for (int i = 0; i < count; i++) {
        pkt = yip.readPacket();
        if (!filterInput(pkt)) {
            if (!packets.add(pkt)) {
                throw new IllegalArgumentException("Unable to add data to the packetQueue!");
            }
        }
    }

    soc.close();

    // Reset idle timeout
    lastFetch = System.currentTimeMillis();
}

From source file:edu.mayo.trilliumbridge.webapp.TransformerController.java

/**
 * From http://stackoverflow.com/a/19137900/656853
 */// ww  w  .  j  a va 2s .  c  om
private InputStream checkStreamIsNotEmpty(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        throw new UserInputException(NO_CONTENT_ERROR_MSG);
    }

    PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
    int b;
    b = pushbackInputStream.read();
    if (b == -1) {
        throw new UserInputException("No file or XML content body sent.");
    }
    pushbackInputStream.unread(b);

    return pushbackInputStream;
}

From source file:org.cruk.genologics.api.GenologicsAPIBatchOperationTest.java

@Test
public void testArtifactBatchUpdate() throws Exception {
    File expectedResultFile = new File("src/test/xml/batchtestreordering-artifacts.xml");
    String expectedReply = FileUtils.readFileToString(expectedResultFile);

    ArtifactBatchFetchResult updateArtifactsFetch = (ArtifactBatchFetchResult) marshaller
            .unmarshal(new StreamSource(expectedResultFile));

    List<Artifact> artifacts = updateArtifactsFetch.getArtifacts();

    // Rearrange these to a different order to that in the file.
    // This means the original XML file loaded above will return in an order
    // not matching either the Links object (below) or the original order
    // of the artifacts.
    Collections.shuffle(artifacts, new Random(997));

    // Copy the URI order as it is now to make sure it doesn't differ
    // after updating the artifacts.
    List<URI> uriOrder = new ArrayList<URI>();

    Links confirmLinks = new Links();
    for (Artifact a : artifacts) {
        uriOrder.add(a.getUri());//  w w w . j  a  v  a 2 s .c  o  m
        confirmLinks.getLinks().add(new Link(a));
    }

    // The Links object that (would) come back should be in a different order.
    Collections.shuffle(confirmLinks.getLinks(), new Random(1024));

    StringWriter linksXML = new StringWriter();
    marshaller.marshal(confirmLinks, new StreamResult(linksXML));

    // Two calls to make here.

    URI updateUri = new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/batch/update");
    URI retrieveUri = new URI("http://limsdev.cri.camres.org:8080/api/v2/artifacts/batch/retrieve");

    // Note: need PushbackInputStream to prevent the call to getBody() being made more than once.
    // See MessageBodyClientHttpResponseWrapper.

    InputStream response1Stream = new PushbackInputStream(
            new ByteArrayInputStream(linksXML.toString().getBytes()));
    InputStream response2Stream = new PushbackInputStream(new ByteArrayInputStream(expectedReply.getBytes()));

    HttpHeaders headers = new HttpHeaders();

    ClientHttpResponse httpResponse1 = EasyMock.createMock(ClientHttpResponse.class);
    EasyMock.expect(httpResponse1.getStatusCode()).andReturn(HttpStatus.OK).anyTimes();
    EasyMock.expect(httpResponse1.getRawStatusCode()).andReturn(HttpStatus.OK.value()).anyTimes();
    EasyMock.expect(httpResponse1.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpResponse1.getBody()).andReturn(response1Stream).once();
    httpResponse1.close();
    EasyMock.expectLastCall().once();

    ClientHttpRequest httpRequest1 = EasyMock.createMock(ClientHttpRequest.class);
    EasyMock.expect(httpRequest1.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpRequest1.getBody()).andReturn(new NullOutputStream()).times(0, 2);
    EasyMock.expect(httpRequest1.execute()).andReturn(httpResponse1).once();

    ClientHttpResponse httpResponse2 = EasyMock.createMock(ClientHttpResponse.class);
    EasyMock.expect(httpResponse2.getStatusCode()).andReturn(HttpStatus.OK).anyTimes();
    EasyMock.expect(httpResponse2.getRawStatusCode()).andReturn(HttpStatus.OK.value()).anyTimes();
    EasyMock.expect(httpResponse2.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpResponse2.getBody()).andReturn(response2Stream).once();
    httpResponse2.close();
    EasyMock.expectLastCall().once();

    ClientHttpRequest httpRequest2 = EasyMock.createMock(ClientHttpRequest.class);
    EasyMock.expect(httpRequest2.getHeaders()).andReturn(headers).anyTimes();
    EasyMock.expect(httpRequest2.getBody()).andReturn(new NullOutputStream()).times(0, 2);
    EasyMock.expect(httpRequest2.execute()).andReturn(httpResponse2).once();

    ClientHttpRequestFactory mockFactory = EasyMock.createStrictMock(ClientHttpRequestFactory.class);
    EasyMock.expect(mockFactory.createRequest(updateUri, HttpMethod.POST)).andReturn(httpRequest1).once();
    EasyMock.expect(mockFactory.createRequest(retrieveUri, HttpMethod.POST)).andReturn(httpRequest2).once();

    restTemplate.setRequestFactory(mockFactory);

    EasyMock.replay(httpResponse1, httpRequest1, httpResponse2, httpRequest2, mockFactory);

    api.updateAll(artifacts);

    assertEquals("Wrong number of artifacts", uriOrder.size(), artifacts.size());

    for (int i = 0; i < uriOrder.size(); i++) {
        assertEquals("Artifact " + i + " wrong:", uriOrder.get(i), artifacts.get(i).getUri());
    }

    EasyMock.verify(httpResponse2, httpRequest2, mockFactory);
}

From source file:org.apache.pig.builtin.Utf8StorageConverter.java

private Object bytesToObject(byte[] b, ResourceFieldSchema fs) throws IOException {
    Object field;/*from  w w w . jav a  2s.  co m*/
    if (DataType.isComplex(fs.getType())) {
        ByteArrayInputStream bis = new ByteArrayInputStream(b);
        PushbackInputStream in = new PushbackInputStream(bis);
        field = consumeComplexType(in, fs);
    } else {
        field = parseSimpleType(b, fs);
    }
    return field;
}

From source file:grails.converters.JSON.java

/**
 * Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
 *
 * @param request the JSON Request//from   w  w  w.ja  v  a  2  s  .  c  om
 * @return ether a JSONObject or a JSONArray - depending on the given JSON
 * @throws ConverterException when the JSON content is not valid
 */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object json = request.getAttribute(CACHED_JSON);
    if (json != null) {
        return json;
    }

    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        PushbackInputStream pushbackInputStream = null;
        int firstByte = -1;
        try {
            pushbackInputStream = new PushbackInputStream(request.getInputStream());
            firstByte = pushbackInputStream.read();
        } catch (IOException ioe) {
        }

        if (firstByte == -1) {
            return new JSONObject();
        }

        pushbackInputStream.unread(firstByte);
        json = parse(pushbackInputStream, encoding);
        request.setAttribute(CACHED_JSON, json);
        return json;
    } catch (IOException e) {
        throw new ConverterException("Error parsing JSON", e);
    }
}

From source file:org.apache.pig.builtin.Utf8StorageConverter.java

@Override
public DataBag bytesToBag(byte[] b, ResourceFieldSchema schema) throws IOException {
    if (b == null)
        return null;
    DataBag db;/*from  ww  w .  ja v a  2  s  .c om*/
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(b);
        PushbackInputStream in = new PushbackInputStream(bis);
        db = consumeBag(in, schema);
    } catch (IOException e) {
        LogUtils.warn(this, "Unable to interpret value " + Arrays.toString(b) + " in field being "
                + "converted to type bag, caught ParseException <" + e.getMessage() + "> field discarded",
                PigWarning.FIELD_DISCARDED_TYPE_CONVERSION_FAILED, mLog);
        return null;
    }
    return db;
}

From source file:com.zimbra.cs.pop3.Pop3Handler.java

private void sendMessage(InputStream is, int maxNumBodyLines) throws IOException {
    boolean inBody = false;
    int numBodyLines = 0;

    PushbackInputStream stream = new PushbackInputStream(is);
    int c;//w  w w  . j a va  2s .c o m

    boolean startOfLine = true;
    int lineLength = 0;

    while ((c = stream.read()) != -1) {
        if (c == '\r' || c == '\n') {
            if (c == '\r') {
                int peek = stream.read();
                if (peek != '\n' && peek != -1)
                    stream.unread(peek);
            }

            if (!inBody) {
                if (lineLength == 0)
                    inBody = true;
            } else {
                numBodyLines++;
            }
            startOfLine = true;
            lineLength = 0;
            output.write(LINE_SEPARATOR);

            if (inBody && numBodyLines >= maxNumBodyLines) {
                break;
            }
            continue;
        } else if (c == TERMINATOR_C && startOfLine) {
            output.write(c); // we'll end up writing it twice
        }
        if (startOfLine)
            startOfLine = false;
        lineLength++;
        output.write(c);
    }
    if (lineLength != 0) {
        output.write(LINE_SEPARATOR);
    }
    output.write(TERMINATOR_BYTE);
    output.write(LINE_SEPARATOR);
    output.flush();
}