Java Code Examples for java.io.PushbackReader#unread()

The following examples show how to use java.io.PushbackReader#unread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PGCopyPreparedStatement.java    From phoebus with Eclipse Public License 1.0 7 votes vote down vote up
@Override
public int[] executeBatch() throws SQLException {
    long res = 0;
    try {
        CopyManager cpManager = ((PGConnection) connection).getCopyAPI();
        PushbackReader reader = new PushbackReader(new StringReader(""),
                batchBuilder.length());
        reader.unread(batchBuilder.toString().toCharArray());
        res = cpManager.copyIn("COPY " + tableName +  " FROM STDIN WITH CSV", reader);
        batchBuilder.setLength(0);
        reader.close();
    } catch (IOException e) {
        throw new SQLException(e);
    }
    return new int[] { (int) res };
}
 
Example 2
Source File: IonBinary.java    From ion-java with Apache License 2.0 6 votes vote down vote up
final boolean isLongTerminator(int terminator, PushbackReader r) throws IOException {
    int c;

    // look for terminator 2 - if it's not there put back what we saw
    c = r.read();
    if (c != terminator) {
        r.unread(c);
        return false;
    }

    // look for terminator 3 - otherwise put back what we saw and the
    // preceeding terminator we found above
    c = r.read();
    if (c != terminator) {
        r.unread(c);
        r.unread(terminator);
        return false;
    }
    // we found 3 in a row - now that's a long terminator
    return true;
}
 
Example 3
Source File: OFXTransactionExtracter.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
private static String getNextValue(PushbackReader stream)
		throws IOException {
	StringBuffer value = new StringBuffer();
	int ch = 0;

	while ((ch = stream.read()) != -1) {
		if (ch == START_TAG) {
			stream.unread(ch);
			break;
		}

		value.append((char) ch);
	}

	return value.toString().trim();
}
 
Example 4
Source File: DTDParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parser that reads the name of entity reference and replace it with
 * the content of that entity (using the pushback capability of input).
 * It gets the control just after starting '%'
 * @returns the name of reference which was replaced. */
private String parseEntityReference( PushbackReader in ) throws IOException, WrongDTDException {
    StringBuffer sb = new StringBuffer();
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) break; // EOF
        if( isNameChar( (char)i ) ) {
            sb.append( (char)i );                 // next char of name
        } else {
            String entValue = (String)entityMap.get( sb.toString() ); //get the entity content
            if( entValue == null )
                throw new WrongDTDException( "No such entity: \"" + sb + "\"" ); // NOI18N
            
            if( i != ';' ) in.unread( i );
            in.unread( entValue.toCharArray() );  // push it back to stream
            return sb.toString();
        }
    }
    throw new WrongDTDException( "Premature end of DTD" ); // NOI18N
}
 
Example 5
Source File: XmlReader.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
String readText(PushbackReader r)
    throws Exception
{
  char c = readNextNonWhitespaceChar(r);
  StringBuffer text = null;
  while (c != '<') {
    if (text == null) {
      text = new StringBuffer();
    }
    if (isXMLEscapeCharacter(c)) {
      c = readEscapedCharacter(r);
    }
    text.append(c);
    c = readNextChar(r);
  }
  r.unread(c);
  if (text == null) {
    return null;
  }
  return text.toString().trim();
}
 
Example 6
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
private Row readLine(PushbackReader in) throws IOException {
    Row ret=new Row();
    while(true){
        int c=in.read();
        if(c<0) {
            if(ret.size()>0) return ret;
            else return null;
        }
        else in.unread(c);

        Object v=readValue(in);
        ret.add(v);
        boolean next=readValueSeparator(in);
        if(!next) break;
    }

    return ret;
}
 
Example 7
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
private Object readValue(PushbackReader in) throws IOException {
    readWhitespace(in);
    while(true){
        int c=in.read();
        if(c==valueSeparator || c==lineSeparator) {
            in.unread(c);
            return null;
        }
        if(c<0){
            return null;
        }
        else if(c==stringChar){
            in.unread(c);
            return parseString(in);
        }
        else {
            in.unread(c);
            return parseNumberOrDate(in);
        }
    }
}
 
Example 8
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private void readWhitespace(PushbackReader in) throws IOException {
    while(true){
        int c=in.read();
        if(c<0) return;
        else if(c!=lineSeparator && Character.isWhitespace(c)) continue;
        else {
            in.unread(c);
            return;
        }
    }
}
 
Example 9
Source File: OldAndroidPushbackReaderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testPushbackReader() throws Exception {
    String str = "AbCdEfGhIjKlMnOpQrStUvWxYz";
    StringReader aa = new StringReader(str);
    StringReader ba = new StringReader(str);
    StringReader ca = new StringReader(str);

    PushbackReader a = new PushbackReader(aa, 5);
    try {
        a.unread("PUSH".toCharArray());
        Assert.assertEquals("PUSHAbCdEfGhIjKlMnOpQrStUvWxYz", read(a));
    } finally {
        a.close();
    }

    PushbackReader b = new PushbackReader(ba, 15);
    try {
        b.unread('X');
        Assert.assertEquals("XAbCdEfGhI", read(b, 10));
    } finally {
        b.close();
    }

    PushbackReader c = new PushbackReader(ca);
    try {
        Assert.assertEquals("bdfhjlnprtvxz", skipRead(c));
    } finally {
        c.close();
    }
}
 
Example 10
Source File: XmlReader.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static char readAndPushbackNextChar(PushbackReader r)
    throws Exception
{
  char c = readNextChar(r);
  r.unread(c);
  return c;
}
 
Example 11
Source File: XmlReader.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
char readAndPushbackNextNonWhitespaceChar(PushbackReader r)
    throws Exception
{
  char c = readNextNonWhitespaceChar(r);
  r.unread(c);
  return c;
}
 
Example 12
Source File: XmlReader.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
boolean isEndTag(PushbackReader r)
    throws Exception
{
  char first = readNextChar(r);
  char second = readNextChar(r);
  r.unread(second);
  r.unread(first);
  return first == '<' && second == '/';
}
 
Example 13
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private String parseIntPart(PushbackReader in) throws IOException {
    StringBuilder sb=new StringBuilder();
    while(true){
        int c=in.read();
        if(c>='0' && c<='9'){
            sb.append((char)c);
        }
        else {
            if(c>=0) in.unread(c);
            if(sb.length()==0) return null;
            else return sb.toString();
        }
    }
}
 
Example 14
Source File: HashTrie.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively lookup the longest key match.
 * @param keyIn Where to read the key from
 * @param pos The position in the key that is being
 *	looked up at this level.
 * @return The Entry assocatied with the longest key
 *	match or null if none exists.
 */
Entry<T> getLongestMatch(PushbackReader keyIn, StringBuilder key) throws IOException
{
	Node<T> nextNode;
	Entry<T> ret;
	int c;
	char ch;
	int prevLen;

	// read next key char and append to key...
	if((c = keyIn.read())<0)
		// end of input, return what we have currently
		return Entry.newInstanceIfNeeded(key,value);
	ch = (char)c;
	prevLen = key.length();
	key.append(ch);

	if((nextNode = getNextNode(ch))==null)
	{	// last in trie... return ourselves
		return Entry.newInstanceIfNeeded(key,value);
	}
	if((ret = nextNode.getLongestMatch(keyIn, key))!=null)
		return ret;

	// undo reading of key char and appending to key...
	key.setLength(prevLen);
	keyIn.unread(c);

	return Entry.newInstanceIfNeeded(key,value);
}
 
Example 15
Source File: Patch.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Reads a line and returns the char sequence for newline */
private static String readLine(PushbackReader r, StringBuffer nl) throws IOException {
    StringBuffer line = new StringBuffer();
    int ic = r.read();
    if (ic == -1) return null;
    char c = (char) ic;
    while (c != '\n' && c != '\r') {
        line.append(c);
        ic = r.read();
        if (ic == -1) break;
        c = (char) ic;
    }
    if (nl != null) {
        nl.append(c);
    }
    if (c == '\r') {
        try {
            ic = r.read();
            if (ic != -1) {
                c = (char) ic;
                if (c != '\n') r.unread(c);
                else if (nl != null) nl.append(c);
            }
        } catch (IOException ioex) {}
    }
    return line.toString();
}
 
Example 16
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private Object parseNumberOrDate(PushbackReader in) throws IOException {
    readWhitespace(in);

    StringBuilder sb=new StringBuilder();
    while(true) {
        int c=in.read();
        if(c==valueSeparator || c==lineSeparator || c<0) {
            if(c>=0) in.unread(c);
            break;
        }
        else sb.append((char)c);
    }
    String s=sb.toString().trim();

    try {
        int i=Integer.parseInt(s);
        return i;
    } 
    catch(NumberFormatException nfe) {
        try {
            double d=Double.parseDouble(s);
            return d;
        }
        catch(NumberFormatException nfe2) {
            try {
                return parseDate(s);
            }
            catch(Exception ex) {
                return s; // FINALLY WHEN EVERYTHING ELSE FAILS RETURN THE STRING AS A VALUE
            }
        }
    }
}
 
Example 17
Source File: DTDParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Parser that takes care of conditional inclusion/exclusion of part
 * of DTD. Gets the control just after "<![" */
private void parseOptional( PushbackReader in ) throws IOException, WrongDTDException {
    int state = OPT_INIT;
    StringBuffer process = new StringBuffer();
    StringBuffer content = new StringBuffer();
    boolean ignore = false;
    
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) break; // EOF
        switch( state ) {
            case OPT_INIT:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '%' ) {
                    parseEntityReference( in );
                    break;
                }
                process.append( (char)i );
                state = OPT_PROCESS;
                break;
                
            case OPT_PROCESS:
                if( Character.isWhitespace( (char)i ) ) {
                    String s = process.toString();
                    if( "IGNORE".equals( s ) ) ignore = true; // NOI18N
                    else if( ! "INCLUDE".equals( s ) ) throw new WrongDTDException( "Unexpected processing instruction " + s ); // NOI18N
                    state = OPT_APROCESS;
                } else {
                    process.append( (char)i );
                }
                break;
                
            case OPT_APROCESS:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '[' ) state = OPT_CONTENT;
                else throw new WrongDTDException( "Unexpected char '" + (char)i + "' in processing instruction." ); // NOI18N
                break;
                
            case OPT_CONTENT:
                if( i == ']' ) state = OPT_BRAC1;
                else content.append( (char)i );
                break;
                
            case OPT_BRAC1:
                if( i == ']' ) state = OPT_BRAC2;
                else {
                    content.append( ']' ).append( (char)i );
                    state = OPT_CONTENT;
                }
                break;
                
            case OPT_BRAC2:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '>' ) {
                    if( !ignore ) in.unread( content.toString().toCharArray() );
                    return;
                }
                throw new WrongDTDException( "Unexpected char '" + (char)i + "' in processing instruction." ); // NOI18N
        }
    }
    
}
 
Example 18
Source File: DTDParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void parseDTD( PushbackReader in ) throws IOException, WrongDTDException {
    int state = DTD_INIT;
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) {
            break;
        }
        switch( state ) {
            case DTD_INIT:
                switch( i ) {
                    case '<':
                        state = DTD_LT;
                        break;
                    case '%':
                        parseEntityReference( in );
                        break; // Stay in DTD_INIT
                }
                break;
                
            case DTD_LT:
                if( i != '!' ) throw new WrongDTDException( "Unexpected char '" + (char)i + "' after '<'" ); // NOI18N
                state = DTD_EXC;
                break;
                
            case DTD_EXC:
                switch( i ) {
                    case '-':
                        state = DTD_MINUS;
                        break;
                    case '[':
                        parseOptional( in );
                        state = DTD_INIT;
                        break;
                    default:
                        in.unread( i );
                        parseMarkup( in );
                        state = DTD_INIT;
                        break;
                }
                break;
                
            case DTD_MINUS:
                if( i != '-' ) throw new WrongDTDException( "Unexpected char '" + (char)i + "' after \"<!-\"" ); // NOI18N
                parseComment( in );
                state = DTD_ACOMMENT;
                break;
                
            case DTD_ACOMMENT:
                if( i != '>' ) throw new WrongDTDException( "Unexpected char '" + (char)i + "' after comment" ); // NOI18N
                state = DTD_INIT;
                break;
                
        }
    }        
    if( state != DTD_INIT ) throw new WrongDTDException( "Premature end of DTD" ); // NOI18N
}
 
Example 19
Source File: XStringBufBase.java    From ModTheSpire with MIT License 3 votes vote down vote up
/**
 * Push a string back on the input stream.
 *
 * @param s  the string
 * @param pb the PushbackReader onto which to push the characters
 *
 * @throws IOException if an I/O error occurs or if the pushback buffer is
 *                     full
 */
private void unread (String s, PushbackReader pb)
    throws IOException
{
    for (int i = s.length() - 1; i >= 0; i--)
        pb.unread (s.charAt (i));
}
 
Example 20
Source File: SchemaParser.java    From pepper-box with Apache License 2.0 2 votes vote down vote up
/**
  * Function gets schema template as input and translates it into series of java statements
  * @param inputSchema
  * @return
  * @throws IOException
  */
public String getProcessedSchema(String inputSchema) throws PepperBoxException {

    try {
        PushbackReader pushbackReader = new PushbackReader(new StringReader(inputSchema), 80);

        int chr;

        StringBuilder token = new StringBuilder();

        StringBuilder processedSchema = new StringBuilder();

        processedSchema.append(PropsKeys.STR_BUILD_OBJ);

        while ((chr = pushbackReader.read()) != -1) {

            if (chr != PropsKeys.OPENING_BRACE) {

                token.append((char) chr);

            } else if ((chr = pushbackReader.read()) != PropsKeys.OPENING_BRACE) {

                pushbackReader.unread(chr);
                token.append(PropsKeys.OPENING_BRACE);

            } else {

                appendStaticString(token, processedSchema);
                token.delete(0, token.length());
                chr = pushbackReader.read();

                while (chr != -1) {

                    if (chr != PropsKeys.CLOSING_BRACE) {

                        token.append((char) chr);
                        chr = pushbackReader.read();

                    } else if ((chr = pushbackReader.read()) != PropsKeys.CLOSING_BRACE) {

                        pushbackReader.unread((char) chr);
                        token.append(PropsKeys.CLOSING_BRACE);

                    } else {

                        processedSchema.append(PropsKeys.STR_APPEND);
                        processedSchema.append(token.toString().replaceAll(PropsKeys.ESC_QUOTE, PropsKeys.DOUBLE_ESC_QUOTE));
                        processedSchema.append(PropsKeys.CLOSING_BRACKET);
                        token.delete(0, token.length());
                        break;

                    }
                }

            }
        }

        appendStaticString(token, processedSchema);
        processedSchema.append(PropsKeys.STR_TOSTRING);

        return processedSchema.toString().replaceAll(PropsKeys.STR_TAB, PropsKeys.ESCAPE_TAB).replaceAll(PropsKeys.STR_NEWLINE, PropsKeys.ESCAPE_NEWLINE).replaceAll(PropsKeys.STR_CARRIAGE_RETURN, PropsKeys.ESCAPE_CARRIAGE_RETURN);
    }catch (IOException e){
        throw new PepperBoxException(e);
    }
 }