Example usage for java.io PushbackInputStream close

List of usage examples for java.io PushbackInputStream close

Introduction

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

Prototype

public synchronized void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    byte[] arrByte = new byte[1024];
    byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    InputStream is = new ByteArrayInputStream(byteArray);
    PushbackInputStream pis = new PushbackInputStream(is);

    try {//  w w  w. j  a v a  2  s  . c o m
        for (int i = 0; i < byteArray.length; i++) {

            arrByte[i] = (byte) pis.read();

            System.out.println((char) arrByte[i]);
        }

        pis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

/**
 * ??????<br>// www.  ja  v a  2s . c o m
 * 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.adl.parsers.dom.ADLDOMParser.java

/**
 * Sets up the file source for the test subject file.
 *
 * @param iFileName file to setup input source for.
 *
 * @return InputSource//from w w  w  .  j ava  2  s.  c  o m
 */
private InputSource setupFileSource(String iFileName) {
    log.debug("setupFileSource()");
    String msgText;
    boolean defaultEncoding = true;
    String encoding = null;
    PushbackInputStream inputStream;
    FileInputStream inFile;

    try {
        File xmlFile = new File(iFileName);
        log.debug(xmlFile.getAbsolutePath());

        if (xmlFile.isFile()) {
            InputSource is = null;

            defaultEncoding = true;
            if (xmlFile.length() > 1) {
                inFile = new FileInputStream(xmlFile);
                inputStream = new PushbackInputStream(inFile, 4);

                // Reads the initial 4 bytes of the file to check for a Byte
                // Order Mark and determine the encoding

                byte bom[] = new byte[4];
                int n, pushBack;
                n = inputStream.read(bom, 0, bom.length);

                // UTF-8 Encoded
                if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
                    encoding = "UTF-8";
                    defaultEncoding = false;
                    pushBack = n - 3;
                }
                // UTF-16 Big Endian Encoded
                else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
                    encoding = "UTF-16BE";
                    defaultEncoding = false;
                    pushBack = n - 2;
                }
                // UTF-16 Little Endian Encoded               
                else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
                    encoding = "UTF-16LE";
                    defaultEncoding = false;
                    pushBack = n - 2;
                }
                // Default encoding
                else {
                    // Unicode BOM mark not found, unread all bytes                  
                    pushBack = n;
                }

                // Place any non-BOM bytes back into the stream
                if (pushBack > 0) {
                    inputStream.unread(bom, (n - pushBack), pushBack);
                }

                if (defaultEncoding == true) { //Reads in ASCII file.
                    FileReader fr = new FileReader(xmlFile);
                    is = new InputSource(fr);
                }
                // Reads the file in the determined encoding
                else {
                    //Creates a buffer with the size of the xml encoded file
                    BufferedReader inStream = new BufferedReader(new InputStreamReader(inputStream, encoding));
                    StringBuffer dataString = new StringBuffer();
                    String s = "";

                    //Builds the encoded file to be parsed
                    while ((s = inStream.readLine()) != null) {
                        dataString.append(s);
                    }

                    inStream.close();
                    inputStream.close();
                    inFile.close();
                    is = new InputSource(new StringReader(dataString.toString()));
                    is.setEncoding(encoding);
                }
            }
            return is;
        } else if ((iFileName.length() > 6)
                && (iFileName.substring(0, 5).equals("http:") || iFileName.substring(0, 6).equals("https:"))) {
            URL xmlURL = new URL(iFileName);
            InputStream xmlIS = xmlURL.openStream();
            InputSource is = new InputSource(xmlIS);
            return is;
        } else {
            msgText = "XML File: " + iFileName + " is not a file or URL";
            log.error(msgText);
        }
    } catch (NullPointerException npe) {
        msgText = "Null pointer exception" + npe;
        log.error(msgText);
    } catch (SecurityException se) {
        msgText = "Security Exception" + se;
        log.error(msgText);
    } catch (FileNotFoundException fnfe) {
        msgText = "File Not Found Exception" + fnfe;
        log.error(msgText);
    } catch (Exception e) {
        msgText = "General Exception" + e;
        log.error(msgText);
    }

    log.debug("setUpFileSource()");

    return new InputSource();
}

From source file:org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter.java

protected Script compileScript(Context cx, Scriptable scope, Source src) throws Exception {
    PushbackInputStream is = new PushbackInputStream(src.getInputStream(), ENCODING_BUF_SIZE);
    try {//from   w w w  .  ja v a 2 s  . c  o m
        String encoding = findEncoding(is);
        Reader reader = encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding);
        reader = new BufferedReader(reader);
        Script compiledScript = cx.compileReader(scope, reader, src.getURI(), 1, null);
        return compiledScript;
    } finally {
        is.close();
    }
}