Android Open Source - EnergyWastingApp Abstract File Writer






From Project

Back to project page EnergyWastingApp.

License

The source code is released under:

Copyright ? 2013-2014 Pekka Ekman <pekka.ekman@aalto.fi> 2013 Babujee Jerome Robin <robin.babujeejerome@aalto.fi> Permission is hereby granted, free of charge, to any person obtaining a ...

If you think the Android project EnergyWastingApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.aalto.pekman.energywastingapp.components;
/*from   w w w.  j a va 2s. c  o m*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.util.Log;

public abstract class AbstractFileWriter extends Component {

  protected static final String filename = "tmpfile";
  protected static final int chunkSize = 65536;

  /** Returns the directory to which the file is written */
  protected abstract File getPath();

  /** Returns maximum file size */
  protected long getMaxFileSize() { return 32 * 1024 * 1024; }


  protected WriterThread thread = null;
  protected volatile boolean stopThread = false;

  protected static final byte[] buffer;
  static {
    buffer = new byte[chunkSize];
    new Random().nextBytes(buffer);
  }

  protected class WriterThread extends Thread {
    
    private void onError() {
      stopThread = true;
      
      context.runOnUiThread(new Runnable() {
        @Override
        public void run() {
          markTurnedOff();
        }
      });
    }
    
    @Override
    public void run() {
      Log.d("AbstractFileWriter thread", "Starting write thread");
      
      File file = new File(getPath(), filename);
      long maxSize = getMaxFileSize();
      
      while (! stopThread) {
        // open file for writing (truncate if it exists)
        FileOutputStream out;
        try {
          out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
          Log.e("AbstractFileWriter thread", e.getMessage(), e);
          onError();
          return;
        }
        Log.d("AbstractFileWriter thread", "File " + file.getAbsolutePath() + " opened");
        
        long size = 0;
        
        try {
          // write to file until size limit reached
          while (! stopThread && size <= maxSize) {
            out.write(buffer, 0, chunkSize);
            out.flush();
            out.getFD().sync();
            
            size += chunkSize;
          }
        } catch (IOException e) {
          Log.e("AbstractFileWriter thread", e.getMessage(), e);
          onError();
        } finally {
          Log.d("AbstractFileWriter thread", "Closing and deleting file (" + size + " bytes written)");
          
          // close and delete file
          try {
            out.close();
          } catch (IOException e) {
            Log.e("AbstractFileWriter thread", "close: " + e.getMessage(), e);
          }
          if (! file.delete()) {
            Log.w("AbstractFileWriter thread",
                "cannot delete " + file.getAbsolutePath());
          }
        }
      }
      
      Log.d("AbstractFileWriter thread",
          "Write thread stopped (" + file.getAbsolutePath() + ")");
    }
  }

  @Override
  public void start() {
    thread = new WriterThread();
    stopThread = false;
    thread.start();
  }

  @Override
  public void stop() {
    stopThread = true;
  }

}




Java Source Code List

fi.aalto.pekman.energywastingapp.MainActivity.java
fi.aalto.pekman.energywastingapp.components.AbstractCamera.java
fi.aalto.pekman.energywastingapp.components.AbstractFileWriter.java
fi.aalto.pekman.energywastingapp.components.AppDirFileWriter.java
fi.aalto.pekman.energywastingapp.components.BlueToothBurn.java
fi.aalto.pekman.energywastingapp.components.CPUBurn.java
fi.aalto.pekman.energywastingapp.components.Component.java
fi.aalto.pekman.energywastingapp.components.Display.java
fi.aalto.pekman.energywastingapp.components.ExtStorageFileWriter.java
fi.aalto.pekman.energywastingapp.components.GPSCoordSearch.java
fi.aalto.pekman.energywastingapp.components.RecordAudio.java
fi.aalto.pekman.energywastingapp.components.StillCamera.java
fi.aalto.pekman.energywastingapp.components.TonePlay.java
fi.aalto.pekman.energywastingapp.components.Vibration.java
fi.aalto.pekman.energywastingapp.components.VideoCamera.java
fi.aalto.pekman.energywastingapp.components.WiFiDataTransfer.java