private void checkDatabaseSize() throws IOException, LimitedSpaceException {
    long size = 0;
    for (int chunk = 0; chunk < DB_CHUNKS; chunk++) {
      InputStream sizeInput = context.getAssets().open(getFileName(DB_NAME, chunk));

      byte[] ibuffer = new byte[1024];
      int length;
      while ((length = sizeInput.read(ibuffer)) > 0) {
        size += length;
      }
      sizeInput.close();
    }

    long buffer = (long) (size * 0.1);
    long free = AvailableSpaceHandler.getAvailableSpaceInBytes(DB_PATH);
    if (size + buffer > free) {
      throw new LimitedSpaceException("Not enough free space.", size + buffer);
    }
  }
  /**
   * Determines an appropriate location for the reference database.
   *
   * <p>1. Is external storage available (mounted) 2. if so, does the db already exist? 3. if not,
   * is there space for the database here? 4. failing the availability of external storage, sort out
   * the location for internal storage 5. if the database is destined for external storage, but
   * exists internally, delete internal copy
   *
   * @return file pointing to where the database is destined to exist
   */
  private File manageDatabase() {
    File tmpFile, retFile = null;
    long dbSize, free;

    // Look for old databases that may still exist, delete them in found
    tmpFile = new File(OLD_DB_PATH, DB_NAME);
    if (tmpFile.exists()) {
      tmpFile.delete();
    }

    // see if external storage is mounted
    try {
      if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        tmpFile = new File(context.getExternalFilesDir(null), DB_NAME);
        if (tmpFile != null) {
          if (tmpFile.exists()) {
            // db already exists
            retFile = tmpFile;
          } else {
            // see if there is enough space to hold the db on
            // external storage
            free = AvailableSpaceHandler.getAvailableSpaceInBytes(tmpFile.getParent());

            try {
              dbSize = calcDatabaseSize();
            } catch (IOException e) {
              // unable to calculate database size
              // proper exception handling for this will take
              // place
              // when
              // the db is created
              dbSize = 0;
            }

            if (dbSize < free) {
              retFile = tmpFile;
            }
          }
        }
      }
    } catch (Exception e) {
      // Trying SD broke, move to using internal
      ErrorReporter.getInstance().putCustomData("Situation", "Failed to write to SD");
      ErrorReporter.getInstance().handleException(e);
    }

    // check internal storage
    tmpFile = new File(context.getFilesDir().getAbsolutePath(), DB_NAME);
    // see if the db exists on internal storage
    if (tmpFile.exists()) {
      if (null != retFile) {
        // if the db is on internal *and* external storage, delete the
        // internal storage copy
        tmpFile.delete();
      }
    }

    // if we got this far with retFile undefined, there either isn't
    // external storage
    // or insufficient space there. default to internal storage
    if (null == retFile) {
      retFile = tmpFile;
    }

    return retFile;
  }