Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1197 SDFWriter and StringWriter incompatibility?

closed
nobody
5
2014-04-11
2011-12-30
Duece99
No

I have noticed that the SDFWriter (CDK 1.4.6) outputs no SDF data for molecules, when the writer given as an argument to the SDFWriter constructor is a StringWriter object.

I think the problem is down to these blocks of code in the SDFWriter.java Source code:

private BufferedWriter writer;

and the constructor:

public SDFWriter(Writer out) {
    if (out instanceof BufferedWriter) {
        writer = (BufferedWriter)out;
    } else {
        writer = new BufferedWriter(out);
    }
    initIOSettings();
}

giving a size to the BufferedWriter constructor in this line, e.g :

        writer = new BufferedWriter(out, 2);

seemed to fix the problem in my case. I don't know whether this is entirely a problem of CDK or if it's something to do with Java itself.

Ed.

Discussion

  • Hi Ed,

    setting the buffer size to just two chars has the effect that is flushes the cache frequently.

    Are you in your code closing the SDFWrtier before your read the content from the StringWriter? You should, and this is how several unit tests work. Closing of the SDFWriter causes the buffer to be flushed.

     
  • Rajarshi Guha
    Rajarshi Guha
    2012-01-22

    I agree with Egon - make sure to close the writer

     
  • John May
    John May
    2014-04-11

    For reference

    public static String toSDfileString(List<IAtomContainer> ms) throws IOException, CDKException {  
      StringWriter sw  = new StringWriter();
      SDFWriter    sdw = new SDFWriter(sw);
      for (IAtomContainer m : ms){
        sdw.write(ia);
      }
      sdw.close();
      // String sdfString = sw.toString(); // needs to be after close
      return sw.toString();
    }
    
     
    Last edit: John May 2014-04-11