Example usage for java.io PushbackReader PushbackReader

List of usage examples for java.io PushbackReader PushbackReader

Introduction

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

Prototype

public PushbackReader(Reader in) 

Source Link

Document

Creates a new pushback reader with a one-character pushback buffer.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create a new PushBack reader based on our string reader
    PushbackReader pr = new PushbackReader(sr);

    try {/*from  w ww .  j a  va 2  s .co  m*/
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);
        }
        // unread a character
        pr.unread('F');

        // read the next char, which is the one we unread
        char c = (char) pr.read();

        System.out.println(c);

        pr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:gridool.sqlet.partitioning.CsvHashPartitioning.java

private static final CsvReader getCsvReader(final String csvPath, final char filedSeparator,
        final char quoteChar) throws GridException {
    final Reader reader;
    try {/*w w w .  j a v  a  2  s.com*/
        FileInputStream fis = new FileInputStream(csvPath);
        FastBufferedInputStream bis = new FastBufferedInputStream(fis, 16384);
        reader = new InputStreamReader(bis, "UTF-8");
    } catch (FileNotFoundException fne) {
        LOG.error(fne);
        throw new GridException("CSV file not found: " + csvPath, fne);
    } catch (UnsupportedEncodingException uee) {
        LOG.error(uee);
        throw new IllegalStateException(uee); // should never happens
    }
    PushbackReader pushback = new PushbackReader(reader);
    return new SimpleCsvReader(pushback, filedSeparator, quoteChar);
}

From source file:net.panthema.BispanningGame.GraphString.java

public static MyGraph read_graph(String str) throws IOException {

    StringReader sr = new StringReader(str);
    PushbackReader pr = new PushbackReader(sr);

    MyGraph g = new MyGraph();

    if (pr.read() != 'V')
        throw (new IOException("Error in Graph String: format error"));

    int Vnum = readInt(pr);

    int Vdesc = pr.read();

    if (Vdesc == ':') {
        // read vertex list with positions
        for (int i = 0; i < Vnum; ++i) {

            if (pr.read() != 'i')
                throw (new IOException("Error in Graph String: format error"));

            Integer v = readInt(pr);
            g.addVertex(v);//from  w w  w  .  j  a  v a2 s  .c  o m

            Double posx = null, posy = null;

            for (int p = pr.read(); p != '/'; p = pr.read()) {

                if (p == 'x') {
                    posx = (double) readInt(pr);
                } else if (p == 'y') {
                    posy = (double) readInt(pr);
                } else {
                    throw (new IOException("Error in Graph String: unknown attribute"));
                }
            }

            if (posx != null && posy != null) {
                if (g.mInitialLayout == null)
                    g.mInitialLayout = new StaticLayout<Integer, MyEdge>(g);

                g.mInitialLayout.setLocation(v, posx, posy);
            }
        }

        // read terminator of vertex list
        if (pr.read() != ';')
            throw (new IOException("Error in Graph String: format error"));
    } else if (Vdesc == ';') {
        // read anonymous vertex list
        for (int i = 0; i < Vnum; ++i) {
            g.addVertex(i);
        }
    } else {
        throw (new IOException("Error in Graph String: format error"));
    }

    // read edge list

    if (pr.read() != 'E')
        throw (new IOException("Error in Graph String: format error"));

    int Enum = readInt(pr);

    if (pr.read() != ':')
        throw (new IOException("Error in Graph String: format error"));

    for (int i = 0; i < Enum; ++i) {

        if (pr.read() != 'i')
            throw (new IOException("Error in Graph String: format error"));

        Integer ei = readInt(pr);
        MyEdge e = new MyEdge(ei);
        Integer tail = null, head = null;

        for (int p = pr.read(); p != '/'; p = pr.read()) {

            if (p == 't') {
                tail = readInt(pr);
            } else if (p == 'h') {
                head = readInt(pr);
            } else if (p == 'c') {
                e.color = readInt(pr);
            } else {
                throw (new IOException("Error in Graph String: unknown attribute"));
            }
        }

        if (tail == null || head == null)
            throw (new IOException("Error in Graph String: missing tail/head attributes"));

        g.addEdge(e, tail, head);
    }

    if (pr.read() != ';')
        throw (new IOException("Error in Graph String: format error"));

    return g;
}

From source file:net.agkn.field_stripe.record.reader.PGTextRecordReader.java

/**
 * @param  reader the <code>Reader</code> from which the record is read. 
 *         This cannot be <code>null</code>.
 *//*from   ww  w.  j  av  a 2s  .com*/
public PGTextRecordReader(final Reader reader) {
    this.reader = new PushbackReader(reader);
}

From source file:com.googlecode.awsms.senders.vodafone.VodafoneWebSender.java

/**
 * Check if the user is logged in to www.vodafone.it
 * //from   w w w.  java 2 s  . c  o m
 * @return true if the user is logged in, false otherwise
 * @throws Exception
 */
private boolean isLoggedIn() throws Exception {
    Log.d(TAG, "this.isLoggedIn()");
    Document document;

    try {
        HttpGet request = new HttpGet("https://www.vodafone.it/190/trilogy/jsp/utility/checkUser.jsp");
        HttpResponse response = httpClient.execute(request, httpContext);

        PushbackReader reader = new PushbackReader(new InputStreamReader(response.getEntity().getContent()));

        // fix wrong XML header 
        int first = reader.read();
        while (first != 60)
            first = reader.read();
        reader.unread(first);

        document = new SAXBuilder().build(reader);
        response.getEntity().consumeContent();

    } catch (JDOMException jdom) {
        throw new Exception(context.getString(R.string.WebSenderProtocolError));
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(context.getString(R.string.WebSenderNetworkError));
    }

    Element root = document.getRootElement();
    Element child = root.getChild("logged-in");
    return child.getValue().equals("true");
}

From source file:com.ebuddy.cassandra.structure.StructureConverter.java

@SuppressWarnings("fallthrough")
private Object decodeBytes(byte[] bytes) {
    if (bytes.length == 0) {
        return "";
    }//from  w  w  w .  j  a v  a2  s  . com

    // look for header char to determine if a JSON object or legacy NestedProperties
    PushbackReader reader = new PushbackReader(
            new InputStreamReader(new ByteArrayInputStream(bytes), UTF8_CHARSET));

    try {
        int firstChar = reader.read();
        switch (firstChar) {
        case '\uFFFF':
            // legacy NestedProperties, obsolete and interpreted now as simply a JSON encoded Map or
            // beginning of a list terminator

            // if the second character is \uFFFF then this is a list terminator value and just return it
            int secondChar = reader.read();
            if (secondChar == '\uFFFF') {
                return Types.LIST_TERMINATOR_VALUE;
            }
            if (secondChar == -1) {
                throw new DataFormatException("Found header FFFF but no data");
            }
            reader.unread(secondChar);
            // fall through and read as a JSON object

        case HEADER_CHAR:
            try {
                return JSON_MAPPER.readValue(reader, Object.class);
            } catch (IOException e) {
                throw new DataFormatException("Could not parse JSON", e);
            }

        default:
            // if no special header, then bytes are just a string
            return new String(bytes, UTF8_CHARSET);
        }
    } catch (IOException ioe) {
        throw new DataFormatException("Could read data", ioe);
    }
}

From source file:com.googlecode.awsms.senders.vodafone.VodafoneWebSender.java

/**
 * Page to be visited before sending a message
 * //  www  . jav a2s .co m
 * @throws Exception
 */
private void doPrecheck() throws Exception {
    Log.d(TAG, "this.doPrecheck()");
    Document document = null;

    try {
        HttpGet request = new HttpGet("https://www.vodafone.it/190/fsms/precheck.do?channel=VODAFONE_DW");
        HttpResponse response = httpClient.execute(request, httpContext);

        PushbackReader reader = new PushbackReader(new InputStreamReader(response.getEntity().getContent()));

        // fix wrong XML header 
        int first = reader.read();
        while (first != 60)
            first = reader.read();
        reader.unread(first);

        document = new SAXBuilder().build(reader);
        response.getEntity().consumeContent();

    } catch (JDOMException jdom) {
        throw new Exception(context.getString(R.string.WebSenderProtocolError));
    } catch (Exception e) {
        throw new Exception(context.getString(R.string.WebSenderNetworkError));
    }

    Element root = document.getRootElement();
    @SuppressWarnings("unchecked")
    List<Element> children = root.getChildren("e");
    int status = 0, errorcode = 0;
    for (Element child : children) {
        //      Log.d(TAG, child.getAttributeValue("n"));
        //      if (child.getAttributeValue("v") != null)
        //        Log.d(TAG, child.getAttributeValue("v"));
        //      if (child.getValue() != null)
        //        Log.d(TAG, child.getValue());
        if (child.getAttributeValue("n").equals("STATUS"))
            status = Integer.parseInt(child.getAttributeValue("v"));
        if (child.getAttributeValue("n").equals("ERRORCODE"))
            errorcode = Integer.parseInt(child.getAttributeValue("v"));
    }

    Log.d(TAG, "status code: " + status);
    Log.d(TAG, "error code: " + errorcode);
    if (status != 1)
        parseError(errorcode);
}

From source file:com.googlecode.awsms.senders.vodafone.VodafoneWebSender.java

/**
 * Prepare the message to be sent.//from  w w  w.j  a  v  a  2s. co  m
 * 
 * @param sms
 * @throws Exception
 * @returns false if CAPTCHA present
 */
private boolean doPrepare(WebSMS sms) throws Exception {
    Log.d(TAG, "this.doPrepare()");
    Document document;

    try {
        HttpPost request = new HttpPost("https://www.vodafone.it/190/fsms/prepare.do?channel=VODAFONE_DW");
        List<NameValuePair> requestData = new ArrayList<NameValuePair>();
        requestData.add(new BasicNameValuePair("receiverNumber", sms.getReceiverNumber()));
        requestData.add(new BasicNameValuePair("message", sms.getMessage()));
        request.setEntity(new UrlEncodedFormEntity(requestData, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(request, httpContext);

        PushbackReader reader = new PushbackReader(new InputStreamReader(response.getEntity().getContent()));

        // fix wrong XML header 
        int first = reader.read();
        while (first != 60)
            first = reader.read();
        reader.unread(first);

        document = new SAXBuilder().build(reader);
        response.getEntity().consumeContent();

    } catch (JDOMException jdom) {
        throw new Exception(context.getString(R.string.WebSenderProtocolError));
    } catch (Exception e) {
        throw new Exception(context.getString(R.string.WebSenderNetworkError));
    }

    Element root = document.getRootElement();
    @SuppressWarnings("unchecked")
    List<Element> children = root.getChildren("e");
    int status = 0, errorcode = 0;
    for (Element child : children) {
        //      Log.d(TAG, child.getAttributeValue("n"));
        //      if (child.getAttributeValue("v") != null)
        //        Log.d(TAG, child.getAttributeValue("v"));
        //      if (child.getValue() != null)
        //        Log.d(TAG, child.getValue());
        if (child.getAttributeValue("n").equals("STATUS"))
            status = Integer.parseInt(child.getAttributeValue("v"));
        if (child.getAttributeValue("n").equals("ERRORCODE"))
            errorcode = Integer.parseInt(child.getAttributeValue("v"));
        if (child.getAttributeValue("n").equals("CODEIMG")) {
            sms.setCaptchaArray(Base64.decode(child.getValue()));
            return false;
        }
    }

    Log.d(TAG, "status code: " + status);
    Log.d(TAG, "error code: " + errorcode);
    if (status != 1)
        parseError(errorcode);

    return true;
}

From source file:gridool.db.partitioning.phihash.csv.normal.CsvPartitioningTask.java

private static final CsvReader getCsvReader(final DBPartitioningJobConf jobConf) throws GridException {
    final String csvPath = jobConf.getCsvFilePath();
    final Reader reader;
    try {//from   w ww. j  a  va  2s  .c o  m
        FileInputStream fis = new FileInputStream(csvPath);
        FastBufferedInputStream bis = new FastBufferedInputStream(fis, csvInputBufSize);
        reader = new InputStreamReader(bis, "UTF-8");
    } catch (FileNotFoundException fne) {
        LOG.error(fne);
        throw new GridException("CSV file not found: " + csvPath, fne);
    } catch (UnsupportedEncodingException uee) {
        LOG.error(uee);
        throw new IllegalStateException(uee); // should never happens
    }
    PushbackReader pushback = new PushbackReader(reader);
    return new SimpleCsvReader(pushback, jobConf.getFieldSeparator(), jobConf.getStringQuote());
}

From source file:com.googlecode.awsms.senders.vodafone.VodafoneWebSender.java

/**
 * Send the message (after decoding the CAPTCHA)
 * /*w  w  w  .j  a v a  2 s.  c o  m*/
 * @param sms
 * @throws Exception
 * @returns false if CAPTCHA still present
 */
private boolean doSend(WebSMS sms) throws Exception {
    Log.d(TAG, "this.doSend()");
    Document document;

    try {
        HttpPost request = new HttpPost("https://www.vodafone.it/190/fsms/send.do?channel=VODAFONE_DW");
        List<NameValuePair> requestData = new ArrayList<NameValuePair>();
        requestData.add(new BasicNameValuePair("verifyCode", sms.getCaptcha()));
        requestData.add(new BasicNameValuePair("receiverNumber", sms.getReceiverNumber()));
        requestData.add(new BasicNameValuePair("message", sms.getMessage()));
        request.setEntity(new UrlEncodedFormEntity(requestData, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(request, httpContext);

        PushbackReader reader = new PushbackReader(new InputStreamReader(response.getEntity().getContent()));

        // fix wrong XML header 
        int first = reader.read();
        while (first != 60)
            first = reader.read();
        reader.unread(first);

        document = new SAXBuilder().build(reader);
        response.getEntity().consumeContent();

    } catch (JDOMException jdom) {
        throw new Exception(context.getString(R.string.WebSenderProtocolError));
    } catch (Exception e) {
        throw new Exception(context.getString(R.string.WebSenderNetworkError));
    }

    Element root = document.getRootElement();
    @SuppressWarnings("unchecked")
    List<Element> children = root.getChildren("e");
    int status = 0, errorcode = 0;
    String returnmsg = null;
    for (Element child : children) {
        //      Log.d(TAG, child.getAttributeValue("n"));
        //      if (child.getAttributeValue("v") != null)
        //        Log.d(TAG, child.getAttributeValue("v"));
        //      if (child.getValue() != null)
        //        Log.d(TAG, child.getValue());
        if (child.getAttributeValue("n").equals("STATUS"))
            status = Integer.parseInt(child.getAttributeValue("v"));
        if (child.getAttributeValue("n").equals("ERRORCODE"))
            errorcode = Integer.parseInt(child.getAttributeValue("v"));
        if (child.getAttributeValue("n").equals("RETURNMSG"))
            returnmsg = child.getValue();
        if (child.getAttributeValue("n").equals("CODEIMG")) {
            sms.setCaptchaArray(Base64.decode(child.getValue()));
            return false;
        }
    }

    Log.d(TAG, "status code: " + status);
    Log.d(TAG, "error code: " + errorcode);
    Log.d(TAG, "return message: " + returnmsg);
    if (status != 1)
        parseError(errorcode);

    return true;
}