Esempio n. 1
0
  /**
   * Reads annotated token.
   *
   * @param in
   * @return readed token.
   * @throws IOException
   * @throws InvalidFormatException
   */
  private String readWord(PushbackReader in) throws IOException, InvalidFormatException {
    int c;
    StringBuilder buffer = new StringBuilder();
    while (true) {
      c = in.read();

      if (c != -1 && backslashAsEscapeChar && c == '\\') {
        c = in.read();
      }

      if (c == ')') {
        break;
      } else if (c == -1) {
        throw new InvalidFormatException();
      }

      buffer.append((char) c);
    }

    in.unread(c);

    String word = buffer.toString();
    if (TRANSFORM_TABLE.containsKey(word)) {
      word = TRANSFORM_TABLE.get(word);
    }
    return word;
  }
Esempio n. 2
0
  /**
   * skip whitespace characters and comments (characters following a "#" on a line). Also, if a
   * skipped comment consists of a single integer, sets <CODE>offset</CODE> to that integer.
   *
   * @param in
   * @return count of skipped characters.
   * @throws IOException
   */
  private int skipWhitespaceAndComment(PushbackReader in) throws IOException {
    int count = 0;
    boolean inComment = false;
    offset = -1;
    int c;
    do {
      c = in.read();
      count++;
      if (c == '#' && !inComment) {
        inComment = true;
        comment.setLength(0);
      } else if (c == '\n' && inComment) {
        try {
          offset = Integer.parseInt(comment.toString().trim());
        } catch (NumberFormatException e) {
        }
        inComment = false;
      } else if (inComment) {
        comment.append((char) c);
      }
    } while ((Character.isWhitespace(c) || inComment) && c != -1);

    if (c != -1) {
      in.unread(c);
    }

    return count - 1;
  }
Esempio n. 3
0
  private void skipComments() throws ParseException, IOException {
    while (true) {
      int firstChar = 0, middleChar = 0;
      skipWhiteSpace();

      try {
        firstChar = input.read();
      } catch (IOException e) {
        throw new ParseException(0);
      }

      if ((char) firstChar != '{') {
        try {
          input.unread(firstChar);
        } catch (IOException e) {
          throw new ParseException(0);
        }
        break;
      }

      do {
        try {
          middleChar = input.read();
        } catch (IOException e) {
          throw new ParseException(0);
        }
        if ((char) middleChar == '{' || middleChar == -1 || middleChar == EOF_RETURN) {
          throw new ParseException(1);
        }
      } while ((char) middleChar != '}');
    }
  }
Esempio n. 4
0
 private static double readDouble(PushbackReader read) throws IOException {
   StringBuffer s = new StringBuffer();
   int count = -1;
   while (true) {
     char ch = (char) read.read();
     // skip spaces
     if (ch == ' ') continue;
     count++;
     // allow a - only if at the beginning
     if (ch == '-' && count == 0) {
       // u.p("negative number");
       s.append(ch);
       continue;
     }
     if ((ch >= '0' && ch <= '9') || ch == '.') {
       // u.p("got double part " + ch);
       s.append(ch);
     } else {
       if (ch != ',') {
         read.unread(ch);
       }
       break;
     }
   }
   return Double.parseDouble(s.toString());
 }
 /**
  * read from file, while letting the user set the maximum label (taxa) label length as
  * non-standard phylip formats now exist
  */
 public static GenotypeTable readBasicAlignments(String file, int maxLabelLength)
     throws IOException {
   PushbackReader input = InputSource.openFile(file);
   GenotypeTable saa = readBasicAlignments(input, maxLabelLength);
   input.close();
   return saa;
 }
Esempio n. 6
0
 private void readWhiteSpace() throws IOException {
   int ch = in.read();
   while (isWhiteSpace(ch)) {
     ch = in.read();
   }
   in.unread(ch);
 }
Esempio n. 7
0
  private char swallowWhiteSpace(char c, PushbackReader pbr) throws java.io.IOException {
    // Hmm .. I need a separate check for '\n'.
    // Surprised it is not considered a space-character
    char separator = c;

    // Swallow white-space and hyphens
    while (true) {
      int i = pbr.read();
      if (i == -1) {
        break;
      } else {
        c = (char) i;
        if (c == '-') {
          separator = '-';
        } else if (c != '\n' && !Character.isSpaceChar(c)) {
          pbr.unread(i);
          break;
        }
      }
    }

    // Normalize all white-space sequences to a single space
    if (separator == '\n' || Character.isSpaceChar(separator)) separator = ' ';

    return separator;
  }
Esempio n. 8
0
 /**
  * Opens a stream from a public and system ID.
  *
  * @param publicID the public ID, which may be null
  * @param systemID the system ID, which is never null
  * @throws java.net.MalformedURLException if the system ID does not contain a valid URL
  * @throws java.io.FileNotFoundException if the system ID refers to a local file which does not
  *     exist
  * @throws java.io.IOException if an error occurred opening the stream
  */
 public Reader openStream(final String publicID, final String systemID)
     throws MalformedURLException, FileNotFoundException, IOException {
   URL url = new URL(currentReader.systemId, systemID);
   if (url.getRef() != null) {
     final String ref = url.getRef();
     if (url.getFile().length() > 0) {
       url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile());
       url = new URL("jar:" + url + '!' + ref);
     } else {
       url = StdXMLReader.class.getResource(ref);
     }
   }
   currentReader.publicId = publicID;
   currentReader.systemId = url;
   final StringBuilder charsRead = new StringBuilder();
   final Reader reader = this.stream2reader(url.openStream(), charsRead);
   if (charsRead.length() == 0) {
     return reader;
   }
   final String charsReadStr = charsRead.toString();
   final PushbackReader pbreader = new PushbackReader(reader, charsReadStr.length());
   for (int i = charsReadStr.length() - 1; i >= 0; i--) {
     pbreader.unread(charsReadStr.charAt(i));
   }
   return pbreader;
 }
Esempio n. 9
0
 /**
  * Look ahead next character.
  *
  * @param in
  * @return readed character
  * @throws IOException
  */
 private int lookAhead(PushbackReader in) throws IOException {
   int c = in.read();
   if (c != -1) {
     in.unread(c);
   }
   return c;
 }
  /**
   * Reads the first line and determines the EOL-style of the file (relies on the style to be
   * consistent, of course).
   *
   * <p>Sets LS as a side-effect.
   *
   * @return the first line without any line separator, leaves the reader positioned after the first
   *     line separator
   * @since Ant 1.8.2
   */
  private String readFirstLine(PushbackReader r) throws IOException {
    StringBuffer sb = new StringBuffer(80);
    int ch = r.read();
    boolean hasCR = false;
    // when reaching EOF before the first EOL, assume native line
    // feeds
    LS = StringUtils.LINE_SEP;

    while (ch >= 0) {
      if (hasCR && ch != '\n') {
        // line feed is sole CR
        r.unread(ch);
        break;
      }

      if (ch == '\r') {
        LS = "\r";
        hasCR = true;
      } else if (ch == '\n') {
        LS = hasCR ? "\r\n" : "\n";
        break;
      } else {
        sb.append((char) ch);
      }
      ch = r.read();
    }
    return sb.toString();
  }
Esempio n. 11
0
  private boolean empty(PushbackReader reader) throws IOException {
    int value = reader.read();

    if (value != -1) {
      reader.unread(value);
    }

    return value == -1;
  }
Esempio n. 12
0
 private String readText() throws IOException {
   StringBuilder sb = new StringBuilder();
   int ch = in.read();
   while (!isWhiteSpace(ch) && isNotLeftParen(ch) && isNotRightParen(ch)) {
     sb.append((char) ch);
     ch = in.read();
   }
   in.unread(ch);
   //      System.out.println("Read text: ["+sb+"]");
   return sb.toString().intern();
 }
Esempio n. 13
0
  /**
   * skip whitespace characters
   *
   * @param in
   * @return count of skipped characters.
   * @throws IOException
   */
  private int skipWhitespace(PushbackReader in) throws IOException {
    int count = 0;
    int c;
    do {
      c = in.read();
      count++;
    } while (Character.isWhitespace(c) && c != -1);

    if (c != -1) {
      in.unread(c);
    }

    return count - 1;
  }
Esempio n. 14
0
 private void skipWhiteSpace() throws ParseException, IOException {
   int currentChar = -1;
   do {
     try {
       currentChar = input.read();
     } catch (IOException e) {
       throw new ParseException(0);
     }
   } while (Character.isWhitespace((char) currentChar));
   try {
     input.unread(currentChar);
   } catch (IOException e2) {
     throw new ParseException(0);
   }
   return;
 }
 /* (non-Javadoc)
  * @see java.io.PushbackReader#unread(int)
  */
 @Override
 public void unread(final int c) throws IOException {
   if (c == '\n') {
     this.lineNumber--;
   }
   super.unread(c);
 }
  private synchronized void shutDown() {
    if (fDebug) System.out.println("shutdown " + fPort); // $NON-NLS-1$

    if (fWriter != null) {
      fWriter.close();
      fWriter = null;
    }
    try {
      if (fPushbackReader != null) {
        fPushbackReader.close();
        fPushbackReader = null;
      }
    } catch (IOException e) {
    }
    try {
      if (fSocket != null) {
        fSocket.close();
        fSocket = null;
      }
    } catch (IOException e) {
    }
    try {
      if (fServerSocket != null) {
        fServerSocket.close();
        fServerSocket = null;
      }
    } catch (IOException e) {
    }
  }
Esempio n. 17
0
 private void readLeftParen() throws IOException {
   //      System.out.println("Read left.");
   readWhiteSpace();
   int ch = in.read();
   if (isNotLeftParen(ch))
     throw new RuntimeException(
         "Format error reading tree with character: (" + Character.valueOf((char) ch) + ")");
 }
Esempio n. 18
0
 private void unread(int character) throws IOException {
   if (character == '\n') {
     line--;
   }
   pushbackReader.unread(character);
   if (pureTextFromFile.getLast() == character) {
     pureTextFromFile.pollLast();
   }
 }
Esempio n. 19
0
  /**
   * Opens a stream from a public and system ID.
   *
   * @param publicID the public ID, which may be null
   * @param systemID the system ID, which is never null
   * @throws java.net.MalformedURLException if the system ID does not contain a valid URL
   * @throws java.io.FileNotFoundException if the system ID refers to a local file which does not
   *     exist
   * @throws java.io.IOException if an error occurred opening the stream
   */
  public Reader openStream(String publicID, String systemID)
      throws MalformedURLException, FileNotFoundException, IOException {
    URL url = new URL(this.currentSystemID, systemID);
    StringBuffer charsRead = new StringBuffer();
    Reader reader = this.stream2reader(url.openStream(), charsRead);

    if (charsRead.length() == 0) {
      return reader;
    }

    String charsReadStr = charsRead.toString();
    PushbackReader pbreader = new PushbackReader(reader, charsReadStr.length());
    for (int i = charsReadStr.length() - 1; i >= 0; i--) {
      pbreader.unread(charsReadStr.charAt(i));
    }

    return pbreader;
  }
 /* (non-Javadoc)
  * @see java.io.PushbackReader#unread(char[], int, int)
  */
 @Override
 public void unread(final char[] cbuf, final int off, final int len) throws IOException {
   for (int i = off; i < off + len; i++) {
     if (cbuf[i] == '\n') {
       this.lineNumber--;
     }
   }
   super.unread(cbuf, off, len);
 }
Esempio n. 21
0
  private int read() throws IOException {
    int character = pushbackReader.read();

    if (!isEOFCharacter(character)) {
      pureTextFromFile.offerLast((char) character);
    }
    if (character == '\n') {
      line++;
    }
    return character;
  }
 public static void main(String[] args) {
   PushbackReader pr = null;
   try {
     // 创建一个PushbackReader对象,指定推回缓冲区的长度为64
     pr = new PushbackReader(new FileReader("PushbackTest.java"), 64);
     char[] buf = new char[32];
     // 用以保存上次读取的字符串内容
     String lastContent = "";
     int hasRead = 0;
     // 循环读取文件内容
     while ((hasRead = pr.read(buf)) > 0) {
       // 将读取的内容转换成字符串
       String content = new String(buf, 0, hasRead);
       int targetIndex = 0;
       // 将上次读取的字符串和本次读取的字符串拼起来,查看是否包含目标字符串
       // 如果包含目标字符串
       if ((targetIndex = (lastContent + content).indexOf("new PushbackReader")) > 0) {
         // 将本次内容和上次内容一起推回缓冲区
         pr.unread((lastContent + content).toCharArray());
         // 再次读取指定长度的内容(就是目标字符串之前的内容)
         pr.read(buf, 0, targetIndex);
         // 打印读取的内容
         System.out.print(new String(buf, 0, targetIndex));
         System.exit(0);
       } else {
         // 打印上次读取的内容
         System.out.print(lastContent);
         // 将本次内容设为上次读取的内容
         lastContent = content;
       }
     }
   } catch (IOException ioe) {
     ioe.printStackTrace();
   } finally {
     try {
       if (pr != null) pr.close();
     } catch (IOException ioe) {
       ioe.printStackTrace();
     }
   }
 }
Esempio n. 23
0
 /**
  * read a number or name.
  *
  * @param ch
  * @return false: neither found.
  * @throws IOException
  */
 private boolean parseNameOrLiteral(int ch) throws IOException {
   if (Character.isLetterOrDigit((char) ch)) {
     string_value.setLength(0);
     string_value.append((char) ch);
     for (; ; ) {
       ch = cin.read();
       pos++;
       if (-1 == ch || !Character.isLetterOrDigit((char) ch)) break;
       string_value.append((char) ch);
     }
     cin.unread(ch);
     final String s = string_value.toString();
     final Matcher mat = floatPat.matcher(s);
     if (mat.matches()) {
       number_value = Double.parseDouble(s);
       curr_tok = NUMBER;
     } else curr_tok = NAME;
     return true;
   }
   return false;
 }
Esempio n. 24
0
  /**
   * Reads a tag name which is after opened parenthesis.
   *
   * @param in
   * @return readed token string
   * @throws IOException
   * @throws InvalidFormatException
   */
  private String readTagName(PushbackReader in) throws IOException, InvalidFormatException {
    StringBuilder buffer = new StringBuilder();
    int c;

    while (true) {
      c = in.read();
      if (c == -1) {
        throw new InvalidFormatException();
      } else if (Character.isWhitespace(c)) {
        break;
      }

      buffer.append((char) c);
    }

    in.unread(c);

    if (buffer.length() == 0) {
      throw new InvalidFormatException();
    }

    return buffer.toString().toLowerCase().intern();
  }
  private static void insertCPI(
      String[] COLUMN_NAMES,
      Connection conn,
      Statement stmt,
      Integer count,
      int batchSize,
      StringBuffer sb,
      CopyManager cpManager,
      PushbackReader reader)
      throws SQLException, ClassNotFoundException {
    // TODO Auto-generated method stub

    Timestamp timestampStart = new Timestamp(System.currentTimeMillis());
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    // dateFormat.format(System.currentTimeMillis());
    // System.out.println("dateFormat : "+dateFormat.format(System.currentTimeMillis()));
    System.out.println("count in insertCPI :" + count);

    // CopyManager cpManager = ((PGConnection)conn).getCopyAPI();
    // PushbackReader reader = new PushbackReader( new StringReader(""), 10000 );
    for (int i = 0; i < COLUMN_NAMES.length; i++) {
      if (i == 0) {
        sb.append("").append(COLUMN_NAMES[0]).append(",");
      } else if (i == COLUMN_NAMES.length - 1) {
        COLUMN_NAMES[i] = dateFormat.format(System.currentTimeMillis());
        sb.append(COLUMN_NAMES[i]).append("");
      } else {
        sb.append(COLUMN_NAMES[i]).append(",");
      }
      /**
       * if(i==0){ sb.append("'").append(COLUMN_NAMES[0]).append("','"); } else
       * if(i==COLUMN_NAMES.length-1){ sb.append(COLUMN_NAMES[i]).append("'"); } else {
       * sb.append(COLUMN_NAMES[i]).append("','"); } *
       */
    }
    sb.append("\n");

    System.out.println("insert string :  " + sb.toString());
    if (count % batchSize == 0) {
      try {
        // System.out.println("insert string batch :  "+sb.toString());
        reader.unread(sb.toString().toCharArray());
        cpManager.copyIn("COPY JOB FROM STDIN WITH CSV", reader);
        sb.delete(0, sb.length());
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
 private String readMessage(PushbackReader in) throws IOException {
   StringBuffer buf = new StringBuffer(128);
   int ch;
   while ((ch = in.read()) != -1) {
     if (ch == '\n') {
       fLastLineDelimiter = "\n"; // $NON-NLS-1$
       return buf.toString();
     } else if (ch == '\r') {
       ch = in.read();
       if (ch == '\n') {
         fLastLineDelimiter = "\r\n"; // $NON-NLS-1$
       } else {
         in.unread(ch);
         fLastLineDelimiter = "\r"; // $NON-NLS-1$
       }
       return buf.toString();
     } else {
       buf.append((char) ch);
     }
   }
   fLastLineDelimiter = null;
   if (buf.length() == 0) return null;
   return buf.toString();
 }
Esempio n. 27
0
  private boolean skipHeader(PushbackReader pbr) throws java.io.IOException {
    char[] signals = "<pre>".toCharArray();
    char trigger = signals[0];
    int endState = signals.length;
    int state = 0;

    while (true) {
      int i = pbr.read();
      if (i == -1) return true;

      char c = (char) i;
      if (state > 0) {
        state = (c == signals[state]) ? state + 1 : 0;
        // Done with header.  We are ready to process text
        if (state == endState) return false;
      } else if (c == trigger) {
        state = 1;
      }
    }
  }
Esempio n. 28
0
 private int get_token() throws IOException, ParseException {
   int ch = 0;
   do {
     ch = cin.read();
     pos++;
     if (-1 == ch) return curr_tok = END;
   } while (Character.isWhitespace((char) ch));
   switch (ch) {
     case 65535:
     case -1:
       return curr_tok = END;
     case '*':
     case '/':
     case '+':
     case '-':
     case '(':
     case ')':
     case '=':
       return curr_tok = ch;
     default:
       if (parseNameOrLiteral(ch)) return curr_tok;
       throw new ParseException("bad token", pos);
   }
 }
Esempio n. 29
0
 private void readRightParen() throws IOException {
   //      System.out.println("Read right.");
   readWhiteSpace();
   int ch = in.read();
   if (isNotRightParen(ch)) throw new RuntimeException("Format error reading tree.");
 }
Esempio n. 30
0
 private int peek() throws IOException {
   int ch = in.read();
   in.unread(ch);
   return ch;
 }