legacy/src/main/java/ucar/nc2/NetcdfFileWriteable.java
private void rewrite() throws IOException {
    // close existing file, rename and open as read-only
    spiw.flush();
    spiw.close();

    File prevFile = new File(location);
    File tmpFile = new File(location+".tmp");
    if (tmpFile.exists()) tmpFile.delete();
    if (!prevFile.renameTo(tmpFile)) {
      System.out.printf("%50s prevFile.exists=%s canRead=%s canWrite=%s%n", prevFile.getPath(), prevFile.exists(), prevFile.canRead(), prevFile.canWrite());
      System.out.printf("%50s  tmpFile.exists=%s canRead=%s canWrite=%s%n", tmpFile.getPath(), tmpFile.exists(), tmpFile.canRead(), tmpFile.canWrite());
      throw new RuntimeException("Cant rename "+prevFile.getAbsolutePath()+" to "+ tmpFile.getAbsolutePath());
    }

    NetcdfFile oldFile = NetcdfFile.open(tmpFile.getPath());

    // use record dimension if it has one
    Structure recordVar = null;
    if (oldFile.hasUnlimitedDimension()) {
      oldFile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      recordVar = (Structure) oldFile.findVariable("record");
      /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      } */
    }

    // create new file with current set of objects
    spiw.create(location, this, extraHeader, preallocateSize, isLargeFile);
    spiw.setFill( fill);
    //isClosed = false;

    // wait till header is written before adding the record variable to the file
    if (recordVar != null) {
      Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      if (!result)
        recordVar = null;
    }

    // copy old file to new
    List<Variable> oldList = new ArrayList<Variable>(getVariables().size());
    for (Variable v : getVariables()) {
      Variable oldVar = oldFile.findVariable(v.getFullNameEscaped());
      if (oldVar != null)
        oldList.add(oldVar);
    }
    FileWriter.copyVarData(this, oldList, recordVar, null);
    flush();

    // delete old
    oldFile.close();
    if (!tmpFile.delete())
      throw new RuntimeException("Cant delete "+location);
  }
private void rewrite() throws IOException {
    // close existing file, rename and open as read-only
    spiw.flush();
    spiw.close();

    File prevFile = new File(location);
    File tmpFile = new File(location+".tmp");
    if (tmpFile.exists()) tmpFile.delete();
    if (!prevFile.renameTo(tmpFile)) {
      System.out.printf("%50s prevFile.exists=%s canRead=%s canWrite=%s%n", prevFile.getPath(), prevFile.exists(), prevFile.canRead(), prevFile.canWrite());
      System.out.printf("%50s  tmpFile.exists=%s canRead=%s canWrite=%s%n", tmpFile.getPath(), tmpFile.exists(), tmpFile.canRead(), tmpFile.canWrite());
      throw new RuntimeException("Cant rename "+prevFile.getAbsolutePath()+" to "+ tmpFile.getAbsolutePath());
    }

    NetcdfFile oldFile = NetcdfFile.open(tmpFile.getPath());

    // use record dimension if it has one
    Structure recordVar = null;
    if (oldFile.hasUnlimitedDimension()) {
      oldFile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      recordVar = (Structure) oldFile.findVariable("record");
      /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      } */
    }

    // create new file with current set of objects
    spiw.create(location, this, extraHeader, preallocateSize, isLargeFile);
    spiw.setFill( fill);
    //isClosed = false;

    // wait till header is written before adding the record variable to the file
    if (recordVar != null) {
      Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      if (!result)
        recordVar = null;
    }

    // copy old file to new
    List<Variable> oldList = new ArrayList<Variable>(getVariables().size());
    for (Variable v : getVariables()) {
      Variable oldVar = oldFile.findVariable(v.getFullNameEscaped());
      if (oldVar != null)
        oldList.add(oldVar);
    }
    FileWriter.copyVarData(this, oldList, recordVar, null);
    flush();

    // delete old
    oldFile.close();
    if (!tmpFile.delete())
      throw new RuntimeException("Cant delete "+location);
  }
private void rewrite ( ) throws IOException { // close existing file, rename and open as read-only spiw . flush ( ) ; spiw . close ( ) ; File prevFile = new File ( location ) ; File tmpFile = new File ( location + ".tmp" ) ; if ( tmpFile . exists ( ) ) tmpFile . delete ( ) ; if ( ! prevFile . renameTo ( tmpFile ) ) { System . out . printf ( "%50s prevFile.exists=%s canRead=%s canWrite=%s%n" , prevFile . getPath ( ) , prevFile . exists ( ) , prevFile . canRead ( ) , prevFile . canWrite ( ) ) ; System . out . printf ( "%50s  tmpFile.exists=%s canRead=%s canWrite=%s%n" , tmpFile . getPath ( ) , tmpFile . exists ( ) , tmpFile . canRead ( ) , tmpFile . canWrite ( ) ) ; throw new RuntimeException ( "Cant rename " + prevFile . getAbsolutePath ( ) + " to " + tmpFile . getAbsolutePath ( ) ) ; } NetcdfFile oldFile = NetcdfFile . open ( tmpFile . getPath ( ) ) ; // use record dimension if it has one Structure recordVar = null ; if ( oldFile . hasUnlimitedDimension ( ) ) { oldFile . sendIospMessage ( NetcdfFile . IOSP_MESSAGE_ADD_RECORD_STRUCTURE ) ; recordVar = ( Structure ) oldFile . findVariable ( "record" ) ; /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      } */ } // create new file with current set of objects spiw . create ( location , this , extraHeader , preallocateSize , isLargeFile ) ; spiw . setFill ( fill ) ; //isClosed = false; // wait till header is written before adding the record variable to the file if ( recordVar != null ) { Boolean result = ( Boolean ) spiw . sendIospMessage ( NetcdfFile . IOSP_MESSAGE_ADD_RECORD_STRUCTURE ) ; if ( ! result ) recordVar = null ; } // copy old file to new List < Variable > oldList = new ArrayList < Variable > ( getVariables ( ) . size ( ) ) ; for ( Variable v : getVariables ( ) ) { Variable oldVar = oldFile . findVariable ( v . getFullNameEscaped ( ) ) ; if ( oldVar != null ) oldList . add ( oldVar ) ; } FileWriter . copyVarData ( this , oldList , recordVar , null ) ; flush ( ) ; // delete old oldFile . close ( ) ; if ( ! tmpFile . delete ( ) ) throw new RuntimeException ( "Cant delete " + location ) ; }
rewrite entire file
rewrite entire file