org.apache.commons.compress.utils.CountingInputStream Java Examples

The following examples show how to use org.apache.commons.compress.utils.CountingInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvroSource.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected void startReading(ReadableByteChannel channel) throws IOException {
  try {
    metadata = readMetadataFromFile(getCurrentSource().getSingleFileMetadata().resourceId());
  } catch (IOException e) {
    throw new RuntimeException(
        "Error reading metadata from file " + getCurrentSource().getSingleFileMetadata(), e);
  }

  long startOffset = getCurrentSource().getStartOffset();
  byte[] syncMarker = metadata.getSyncMarker();
  long syncMarkerLength = syncMarker.length;

  if (startOffset != 0) {
    // Rewind order to find the sync marker ending the previous block.
    long position = Math.max(0, startOffset - syncMarkerLength);
    ((SeekableByteChannel) channel).position(position);
    startOffset = position;
  }

  // Satisfy the post condition.
  stream = createStream(channel);
  countStream = new CountingInputStream(stream);
  synchronized (progressLock) {
    currentBlockOffset = startOffset + advancePastNextSyncMarker(stream, syncMarker);
    currentBlockSizeBytes = 0;
  }
}
 
Example #2
Source File: FileLineIndex.java    From bboxdb with Apache License 2.0 4 votes vote down vote up
/**
 * Index the given file
 * @throws IOException
 */
public void indexFile() throws IOException {
	final File file = new File(filename);

	if(! file.canRead()) {
		throw new IOException("Unable to open file: " + filename);
	}

	openDatabase();
	logger.info("Indexing file: {}", filename);

	indexedLines = 1;
	registerLine(indexedLines, 0);
	indexedLines++;

	try (
			final CountingInputStream inputStream
			= new CountingInputStream(new BufferedInputStream(new FileInputStream(file)))
		) {

		int bytesAvailabe = inputStream.available();

		while(bytesAvailabe > 0) {

			final char readChar = (char) inputStream.read();

			if(readChar == '\n') {
				registerLine(indexedLines, inputStream.getBytesRead());
				indexedLines++;
			}

			bytesAvailabe--;

			if(bytesAvailabe == 0) {
				bytesAvailabe = inputStream.available();
			}
		}
	}

	logger.info("Indexing file {} done. Indexed {} lines.",
			filename, getIndexedLines());
}
 
Example #3
Source File: ManLocalArchiveFragment.java    From Man-Man with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Load archive to app data folder from my GitHub releases page
 */
private void downloadArchive() {
    if(mLocalArchive.exists()) {
        return;
    }

    if(!mUserAgreedToDownload) {
        new AlertDialog.Builder(getActivity())
                .setTitle(R.string.confirm_action)
                .setMessage(R.string.confirm_action_load_archive)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mUserAgreedToDownload = true;
                        downloadArchive();
                    }
                }).setNegativeButton(android.R.string.no, null)
                .create().show();
        return;
    }

    // kind of stupid to make a loader just for oneshot DL task...
    // OK, let's do it old way...
    AsyncTask<String, Long, Void> dlTask = new AsyncTask<String, Long, Void>() {

        private Exception possibleEncountered;
        private ProgressDialog pd;

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(getActivity(),
                    getString(R.string.downloading),
                    getString(R.string.please_wait), true);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(params[0]).build();
                Response response = client.newCall(request).execute();
                if (!response.isSuccessful()) {
                    publishProgress(-2L);
                    return null;
                }

                Long contentLength = response.body().contentLength();
                CountingInputStream cis = new CountingInputStream(response.body().byteStream());
                FileOutputStream fos = new FileOutputStream(mLocalArchive);
                byte[] buffer = new byte[8096];
                int read;
                while ((read = cis.read(buffer)) != -1) {
                    fos.write(buffer, 0, read);
                    publishProgress(cis.getBytesRead() * 100 / contentLength);
                }
                fos.close();
                cis.close();
            } catch (IOException e) {
                Log.e(Utils.MM_TAG, "Exception while downloading man pages archive", e);
                possibleEncountered = e;
                publishProgress(-1L);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Long... values) {
            if(values[0] == -1) { // exception
                Utils.showToastFromAnyThread(getActivity(), possibleEncountered.getLocalizedMessage());
            }

            if(values[0] == -2) { // response is not OK
                Utils.showToastFromAnyThread(getActivity(), R.string.no_archive_on_server);
            }

            pd.setMessage(getString(R.string.please_wait) + " " + values[0] + "%");
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            pd.dismiss();
            getLoaderManager().getLoader(MainPagerActivity.LOCAL_PACKAGE_LOADER).onContentChanged();
        }
    };
    dlTask.execute(LOCAL_ARCHIVE_URL);
}