edu.upc.dama.dex.core
Class TextStream

java.lang.Object
  extended by edu.upc.dama.dex.core.TextStream
All Implemented Interfaces:
java.io.Closeable

public class TextStream
extends java.lang.Object
implements java.io.Closeable

Stream to read or write Value.TEXT attributes.

Here there is an example about how to use it:

        import edu.upc.dama.dex.core.*;
        import edu.upc.dama.dex.io.*;
        import java.io.*;


        public class TextStreamSample {

        public static void main(String[] args) throws FileNotFoundException, IOException {
                DEX dex = new DEX();
                GraphPool gpool = null;
                Session sess = null;
                DbGraph dbg = null;
                int doc = Graph.INVALID_TYPE;
                long id = Graph.INVALID_ATTRIBUTE;
                long text = Graph.INVALID_ATTRIBUTE;
                String [] line = null;
                Value v = new Value();
                //
                // Load data
                //
                gpool = dex.create("abstracts.dex");
                sess = gpool.newSession();
                dbg = sess.getDbGraph();
                doc = dbg.newNodeType("DOC");
                id = dbg.newAttribute(doc, "ID", Value.LONG);
                text = dbg.newAttribute(doc, "TEXT", Value.TEXT, Graph.ATTR_KIND_BASIC);
                CSVReader csvr = new CSVReader(new File("abstracts_ES.txt"), '|');
                line = null;
                System.out.println("LOADING DOCS...");
                while ((line = csvr.readNext()) != null) {
                    long oid = dbg.newNode(doc);
                    v.setLong(Long.parseLong(line[0]));
                    dbg.setAttribute(oid, id, v);
                    TextStream tstrm = new TextStream();
                    v.setTextStream(tstrm);
                    dbg.setAttribute(oid, text, v);
                    tstrm.write(line[1].toCharArray(), 0, line[1].length());
                    tstrm.close();
                }
                csvr.close();
                gpool.dumpStorage(sess, "abstracts.storage");
                sess.close();
                gpool.close();
                //
                // Retrieve data
                //
                CSVWriter csvw = new CSVWriter(new FileWriter("tmp.txt"), '|');
                line = new String[2];
                gpool = dex.open("abstracts.dex");
                sess = gpool.newSession();
                dbg = sess.getDbGraph();
                doc = dbg.findType("DOC");
                id = dbg.findAttribute(doc, "ID");
                text = dbg.findAttribute(doc, "TEXT");
                Objects objs = dbg.select(doc);
                Objects.Iterator it = objs.iterator();
                System.out.println("RETRIEVING DOCS...");
                while (it.hasNext()) {
                    long oid = it.next();
                    dbg.getAttribute(oid, id, v);
                    line[0] = v.toString();
                    dbg.getAttribute(oid, text, v);
                    TextStream tstrm = v.getTextStream();
                    StringBuffer sb = new StringBuffer();
                    int length = 0;
                    char[] buff = new char[200];
                    int len = 0;
                    while ((len = tstrm.read(buff, 0, 200)) > 0) {
                        sb.append(buff, 0, len);
                    }
                    tstrm.close();
                    line[1] = sb.toString();
                    csvw.writeNext(line);
                }
                it.close();
                objs.close();
                csvw.close();
                sess.close();
                gpool.close();
                //
                // End
                //
                System.out.print("END!");
                dex.close();
            }

        }
 

Author:
Sparsity Technologies

Nested Class Summary
static class TextStream.TextReader
          Reader implementation for TextStream.
static class TextStream.TextWriter
          Writer implementation for TextStream.
 
Constructor Summary
TextStream()
          Creates a new instance of TextStream.
TextStream(boolean append)
          Creates a new instance of TextStream.
 
Method Summary
 void close()
          Closes the stream.
 void flush()
          Operation not supported!
 int read(char[] cbuf, int off, int len)
          Reads a maximum of count characters from the current stream into buffer, beginning at index.
 void write(char[] cbuf, int off, int len)
          Writes an array of characters to the given stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextStream

public TextStream(boolean append)
Creates a new instance of TextStream.

User will only be able to create just write TextStream instance by means of this constructor.

Parameters:
append - In case of existing data, data will be added if true and replaced otherwise.

TextStream

public TextStream()
Creates a new instance of TextStream.

The stream will replace all existing data.

Method Detail

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Reads a maximum of count characters from the current stream into buffer, beginning at index.

Parameters:
cbuf - The character array which, when the method returns, contains the characters read from the current source between off and (off + len - 1).
off - The index of buffer at which to begin writing.
len - The maximum number of characters to read.
Returns:
The number of characters that have been read.
Throws:
java.io.IOException

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Writes an array of characters to the given stream.

Parameters:
cbuf - A character array containing the data to write.
off - The index into buffer at which to begin writing.
len - The number of characters to write from buffer.
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Operation not supported!

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the stream.

Once a stream has been closed, further method invocations will throw an IOException. Closing a previously-closed stream, however, has no effect.

Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException