<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: io code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 17:11:47 GMT</pubDate>
    <description>DZone Snippets: io code</description>
    <item>
      <title>IoPeg</title>
      <link>http://snippets.dzone.com/posts/show/5687</link>
      <description>This is a direct port of the IoPeg metagrammar (rev 46) as found here: http://iopeg.googlecode.com/svn/trunk/PEG.peg&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#  Hierarchical syntax&lt;br /&gt;grammar IoPeg&lt;br /&gt;&lt;br /&gt;  rule grammar&lt;br /&gt;    spacing definition+ end_of_file&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule definition&lt;br /&gt;    identifier leftarrow expression&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule expression&lt;br /&gt;    sequence (slash sequence)*&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule sequence&lt;br /&gt;    prefix*&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule prefix&lt;br /&gt;    (and / not)? suffix&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule suffix&lt;br /&gt;    primary (question / star / plus)?&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule primary&lt;br /&gt;    identifier !leftarrow / open expression close / literal / class / dot&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule identifier&lt;br /&gt;    ident_start ident_cont* spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule ident_start&lt;br /&gt;    [a-zA-Z_]&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule ident_cont&lt;br /&gt;    ident_start / [0-9]&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule literal&lt;br /&gt;    ['] (!['] char)* ['] spacing / ["] (!["] char)* ["] spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule class&lt;br /&gt;    '[' (!']' range)* ']' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule range&lt;br /&gt;    char '-' char / char&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule char&lt;br /&gt;    "\\" [nrt'"\[\]\\] / "\\" [0-2] [0-7] [0-7] / "\\" [0-7] [0-7]? / !"\\" .&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule leftarrow&lt;br /&gt;    '&lt;-' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule slash&lt;br /&gt;    '/' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule and&lt;br /&gt;    '&amp;' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule not&lt;br /&gt;    '!' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule question&lt;br /&gt;    '?' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule star&lt;br /&gt;    '*' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule plus&lt;br /&gt;    '+' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule open&lt;br /&gt;    '(' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule close&lt;br /&gt;    ')' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule dot&lt;br /&gt;    '.' spacing&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule spacing&lt;br /&gt;    (space / comment)*&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule comment&lt;br /&gt;    '#' (!end_of_line .)* end_of_line&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule space&lt;br /&gt;    ' ' / "\t" / end_of_line&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule end_of_line&lt;br /&gt;    "\r\n" / "\n" / "\r"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  rule end_of_file&lt;br /&gt;    !.&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 Jun 2008 04:29:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5687</guid>
      <author>db-keen (db)</author>
    </item>
    <item>
      <title>Speeding up Java I/O - Read method</title>
      <link>http://snippets.dzone.com/posts/show/5510</link>
      <description>The following example counts the number of newline bytes ('\n') in a file. It simply uses the read method on a FileInputStream: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; import java.io.*;&lt;br /&gt;  &lt;br /&gt;  public class intro1 {&lt;br /&gt;    public static void main(String args[]) {&lt;br /&gt;      if (args.length != 1) {&lt;br /&gt;        System.err.println("missing filename");&lt;br /&gt;        System.exit(1);&lt;br /&gt;      }&lt;br /&gt;      try {&lt;br /&gt;        FileInputStream fis =&lt;br /&gt;            new FileInputStream(args[0]);&lt;br /&gt;        int cnt = 0;&lt;br /&gt;        int b;&lt;br /&gt;        while ((b = fis.read()) != -1) {&lt;br /&gt;          if (b == '\n')&lt;br /&gt;            cnt++;&lt;br /&gt;        }&lt;br /&gt;        fis.close();&lt;br /&gt;        System.out.println(cnt);&lt;br /&gt;      }&lt;br /&gt;      catch (IOException e) {&lt;br /&gt;        System.err.println(e);&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Subject to Sun's &lt;a href="http://developers.sun.com/license/berkeley_license.html"&gt;Code Sample License&lt;/a&gt;</description>
      <pubDate>Sat, 17 May 2008 14:49:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5510</guid>
      <author>rick (Rick Ross)</author>
    </item>
    <item>
      <title>Ruby: Open a file, write to it, and close it in one line</title>
      <link>http://snippets.dzone.com/posts/show/5051</link>
      <description>r - Open a file for reading. The file must exist.&lt;br /&gt;w - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.&lt;br /&gt;a - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.&lt;br /&gt;r+ - Open a file for update both reading and writing. The file must exist.&lt;br /&gt;w+ - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.&lt;br /&gt;a+ - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;File.open(local_filename, 'w') {|f| f.write(doc) }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 29 Jan 2008 18:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5051</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Loading a text file into a String from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4480</link>
      <description>Locate a text file in the classpath and read it into a String&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    /** &lt;br /&gt;     * @param filePath      name of file to open. The file can reside&lt;br /&gt;     *                      anywhere in the classpath&lt;br /&gt;     */&lt;br /&gt;    private String readFileAsString(String filePath) throws java.io.IOException {&lt;br /&gt;        StringBuffer fileData = new StringBuffer(1000);&lt;br /&gt;        BufferedReader reader = new BufferedReader(new InputStreamReader(this&lt;br /&gt;            .getClass().getClassLoader().getResourceAsStream(filePath)));&lt;br /&gt;        char[] buf = new char[1024];&lt;br /&gt;        int numRead = 0;&lt;br /&gt;        while ((numRead = reader.read(buf)) != -1) {&lt;br /&gt;            String readData = String.valueOf(buf, 0, numRead);&lt;br /&gt;            fileData.append(readData);&lt;br /&gt;            buf = new char[1024];&lt;br /&gt;        }&lt;br /&gt;        reader.close();&lt;br /&gt;        return fileData.toString();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:48:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4480</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Loading a property file from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4479</link>
      <description>Loads a property file located anywhere in the classpath&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    private Properties getPropertiesFromClasspath(String propFileName) throws IOException {&lt;br /&gt;        // loading xmlProfileGen.properties from the classpath&lt;br /&gt;        Properties props = new Properties();&lt;br /&gt;        InputStream inputStream = this.getClass().getClassLoader()&lt;br /&gt;            .getResourceAsStream(propFileName);&lt;br /&gt;&lt;br /&gt;        if (inputStream == null) {&lt;br /&gt;            throw new FileNotFoundException("property file '" + propFileName&lt;br /&gt;                + "' not found in the classpath");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        props.load(inputStream);&lt;br /&gt;&lt;br /&gt;        return props;&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4479</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>SCGI connector (Java)</title>
      <link>http://snippets.dzone.com/posts/show/4304</link>
      <description>&lt;code&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.nio.ByteBuffer;&lt;br /&gt;import java.nio.charset.Charset;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * SCGI connector.&lt;br&gt;&lt;br /&gt; * Version: 1.0&lt;br&gt;&lt;br /&gt; * Home page: http://snippets.dzone.com/posts/show/4304&lt;br /&gt; */&lt;br /&gt;public class SCGI {&lt;br /&gt;  public static class SCGIException extends IOException {&lt;br /&gt;    private static final long serialVersionUID = 1L;&lt;br /&gt;&lt;br /&gt;    public SCGIException(String message) {&lt;br /&gt;      super(message);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /** Used to decode the headers. */&lt;br /&gt;  public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Read the &lt;a href="http://python.ca/scgi/protocol.txt"&gt;SCGI&lt;/a&gt; request headers.&lt;br&gt;&lt;br /&gt;   * After the headers had been loaded,&lt;br /&gt;   * you can read the body of the request manually from the same {@code input} stream:&lt;pre&gt;&lt;br /&gt;   *   // Load the SCGI headers.&lt;br /&gt;   *   Socket clientSocket = socket.accept();&lt;br /&gt;   *   BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream(), 4096);&lt;br /&gt;   *   HashMap&lt;String, String&gt; env = SCGI.parse(bis);&lt;br /&gt;   *   // Read the body of the request.&lt;br /&gt;   *   bis.read(new byte[Integer.parseInt(env.get("CONTENT_LENGTH"))]);&lt;br /&gt;   * &lt;/pre&gt;&lt;br /&gt;   * @param input an efficient (buffered) input stream.&lt;br /&gt;   * @return strings passed via the SCGI request.&lt;br /&gt;   */&lt;br /&gt;  public static HashMap parse(InputStream input) throws IOException {&lt;br /&gt;    StringBuilder lengthString = new StringBuilder(12);&lt;br /&gt;    String headers = "";&lt;br /&gt;    for (;;) {&lt;br /&gt;      char ch = (char) input.read();&lt;br /&gt;      if (ch &gt;= '0' &amp;&amp; ch &lt;= '9') {&lt;br /&gt;        lengthString.append(ch);&lt;br /&gt;      } else if (ch == ':') {&lt;br /&gt;        int length = Integer.parseInt(lengthString.toString());&lt;br /&gt;        byte[] headersBuf = new byte[length];&lt;br /&gt;        int read = input.read(headersBuf);&lt;br /&gt;        if (read != headersBuf.length)&lt;br /&gt;          throw new SCGIException("Couldn't read all the headers (" + length + ").");&lt;br /&gt;        headers = ISO_8859_1.decode(ByteBuffer.wrap(headersBuf)).toString();&lt;br /&gt;        if (input.read() != ',') throw new SCGIException("Wrong SCGI header length: " + lengthString);&lt;br /&gt;        break;&lt;br /&gt;      } else {&lt;br /&gt;        lengthString.append(ch);&lt;br /&gt;        throw new SCGIException("Wrong SCGI header length: " + lengthString);&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    HashMap env = new HashMap();&lt;br /&gt;    while (headers.length() != 0) {&lt;br /&gt;      int sep1 = headers.indexOf(0);&lt;br /&gt;      int sep2 = headers.indexOf(0, sep1 + 1);&lt;br /&gt;      env.put(headers.substring(0, sep1), headers.substring(sep1 + 1, sep2));&lt;br /&gt;      headers = headers.substring(sep2 + 1);&lt;br /&gt;    }&lt;br /&gt;    return env;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Jul 2007 13:50:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4304</guid>
      <author>ArtemGr (Artem Gr Kozarezov)</author>
    </item>
    <item>
      <title>"Unget string" function</title>
      <link>http://snippets.dzone.com/posts/show/4252</link>
      <description>As a complement to ungetc(), this C function pushes a string back onto an input stream, character by character.  It returns the number of characters pushed, or -1 if an error occurred.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void ungets(char* str, FILE* stream) {&lt;br /&gt; if (!str || !file) return -1;&lt;br /&gt; size_t len = strlen(str);&lt;br /&gt; for (int i=len-1; i&gt;=0; i--) if (ungetc(str[i], stream) == EOF) return -1;&lt;br /&gt; return len;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:19:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4252</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Java: File System: Getting the path of TMP directory from Environmental Variable</title>
      <link>http://snippets.dzone.com/posts/show/4036</link>
      <description>&lt;code&gt;&lt;br /&gt;public static String INDEX_FILE_LOCATION = System.getenv("TMP") + File.separator + "dl-index.tmp";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 10:54:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4036</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java: Listing the Files or Subdirectories in a Directory</title>
      <link>http://snippets.dzone.com/posts/show/4028</link>
      <description>// Ref: Java Developers Almanac&lt;br /&gt;// http://www.exampledepot.com/egs/java.io/GetFiles.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;    //&lt;br /&gt;    // Example 1&lt;br /&gt;    //&lt;br /&gt;&lt;br /&gt;    File dir = new File("directoryName");&lt;br /&gt;    &lt;br /&gt;    String[] children = dir.list();&lt;br /&gt;    if (children == null) {&lt;br /&gt;        // Either dir does not exist or is not a directory&lt;br /&gt;    } else {&lt;br /&gt;        for (int i=0; i&lt;children.length; i++) {&lt;br /&gt;            // Get filename of file or directory&lt;br /&gt;            String filename = children[i];&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //&lt;br /&gt;    // Example 2&lt;br /&gt;    //&lt;br /&gt;    &lt;br /&gt;    // It is also possible to filter the list of returned files.&lt;br /&gt;    // This example does not return any files that start with '.'&lt;br /&gt;    FilenameFilter filter = new FilenameFilter() {&lt;br /&gt;        public boolean accept(File dir, String name) {&lt;br /&gt;            return !name.startsWith(".");&lt;br /&gt;        }&lt;br /&gt;    };&lt;br /&gt;    children = dir.list(filter);&lt;br /&gt;    &lt;br /&gt;    //&lt;br /&gt;    // Example 3&lt;br /&gt;    //&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;    // The list of files can also be retrieved as File objects&lt;br /&gt;    File[] files = dir.listFiles();&lt;br /&gt;&lt;br /&gt;    //&lt;br /&gt;    // Example 4&lt;br /&gt;    //&lt;br /&gt;    &lt;br /&gt;    // This filter only returns directories&lt;br /&gt;    FileFilter fileFilter = new FileFilter() {&lt;br /&gt;        public boolean accept(File file) {&lt;br /&gt;            return file.isDirectory();&lt;br /&gt;        }&lt;br /&gt;    };&lt;br /&gt;    files = dir.listFiles(fileFilter);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 May 2007 03:38:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4028</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java DOM : Creating an XML Document from XML File</title>
      <link>http://snippets.dzone.com/posts/show/4010</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;      //&lt;br /&gt;      // Create the XML Document&lt;br /&gt;      //&lt;br /&gt;&lt;br /&gt;      DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();&lt;br /&gt;      DocumentBuilder docBuilder = dbfac.newDocumentBuilder();&lt;br /&gt;      Document doc = docBuilder.parse(filePath);&lt;br /&gt;&lt;br /&gt;      // ...&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;    catch (Exception e)&lt;br /&gt;    {&lt;br /&gt;      // ...&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 08:52:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4010</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
  </channel>
</rss>
