Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2015 The University of Vermont and State
   * Agricultural College.  All rights reserved.
   * Written by Matthew B. Storer <>
   * This file is part of CCTS Common.
   * CCTS Common is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  * CCTS Common is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * GNU General Public License for more details.
  * You should have received a copy of the GNU General Public License
  * along with CCTS Common.  If not, see <>.
 package edu.uvm.ccts.common.db.parser;
 import java.util.List;

This class acts as a logical buffer between parsed records and the files into which those records are stored.
 public class TableData {
     private static final Log log = LogFactory.getLog(TableData.class);
     private static final String FIELD_DELIM = "\t";
     private static final String LINE_DELIM = "\n";
     private static final int BYTE_THRESHOLD = 500000;
     private List<Stringlist = new ArrayList<String>();
     private int byteCount = 0;
     private String tempFilename;
     private String filename;

Creates a new TableData buffer.

tempDir the name of a directory into which temporary files will be written
outputDir the name of the target directory into which the finalized data will be written
filename the name of the file
     public TableData(String tempDirString outputDirString filenamethrows IOException {
         this. = tempDir + "/" + filename;
         this. = outputDir + "/" + filename;
     public String getFilename() {
         return ;

Adds a new record to this table. Records may consist of one or more Objects, each of which will be serialized to string before being written to disk. Serialization occurs simply by calling toString() on any Object that is not already a java.lang.String. null values are permitted.

recordParts one or more field values, all of which together represents a single record
     public void addRecord(Object ... recordPartsthrows IOException {
         if (recordParts != null && recordParts.length > 0 && anyNotNull(recordParts)) {
             String[] arr = new String[recordParts.length];
             for (int i = 0; i < recordParts.lengthi ++) {
                 if (recordParts[i] == null) {
                     arr[i] = "\\N";                         // null character
                 } else if (recordParts[iinstanceof String) {
                     String s = (StringrecordParts[i];
                     arr[i] = s.contains("\\") ? s.replaceAll("\\\\""\\\\\\\\") : s;
                 } else if (recordParts[iinstanceof Boolean) {
                     arr[i] = (BooleanrecordParts[i] ? "1" : "0";
                 } else {
                     arr[i] = String.valueOf(recordParts[i]);

Flushes the contents of the internal buffer to disk. This function is called whenever the internal buffer exceeds BYTE_THRESHOLD bytes. All flushed data is written to a temporary file; finalizeUpdates must be called to dump the contents of the temporary file to the master file.

    public void flush() throws IOException {
        OutputStream output = new BufferedOutputStream(new FileOutputStream(true));
        try {
            for (String s : ) {
                output.write((s + ).getBytes());
        } finally {
             = 0;
            try { output.flush(); } catch (Exception e) {}
            try { output.close(); } catch (Exception e) {}

Copies the contents of the temporary / working file to the authoritative / master file. Note that data is not written directly to the master file on calls to flush to prevent the corruption of the authoritative / master file, should a processing error require the system to be restarted, and processing to be resumed from where it left off.

    public void finalizeUpdates() throws IOException {
        try {
        } catch (IOException e) {
            .error("caught " + e.getClass().getName() + " finalizing updates to file '" +  +
                    "' - the contents may have been corrupted!"e);
            throw e;
        } catch (RuntimeException e) {
            .error("caught " + e.getClass().getName() + " finalizing updates to file '" +  +
                    "' - the contents may have been corrupted!"e);
            throw e;
        } finally {
// private methods

Adds a record to the internal buffer. Will trigger a call to flush if the added record causes the internal buffer to exceed BYTE_THRESHOLD bytes in size.

    private void add(String sthrows IOException {
        if (s != null) {
             += s.length();
            if ( >= ) {


parts an array of java.lang.Objects
true if any array item is not null; false otherwise (if all array items are null).
    private boolean anyNotNull(Object[] parts) {
        for (Object part : parts) {
            if (part != nullreturn true;
        return false;
New to GrepCode? Check out our FAQ X