All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yandex.disk.rest.RequestBodyProgress Maven / Gradle / Ivy

/*
* Copyright (c) 2015 Yandex
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yandex.disk.rest;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.internal.Util;
import com.yandex.disk.rest.exceptions.CancelledUploadingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import okio.Buffer;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/* package */ abstract class RequestBodyProgress {

    private static final Logger logger = LoggerFactory.getLogger(RequestBodyProgress.class);

    private static final int SIZE = 2048;

    /**
     * Returns a new request body that transmits the content of {@code file}.
     * 
* Based on {@link RequestBody#create(com.squareup.okhttp.MediaType, java.io.File)} * * @see RequestBody#create(com.squareup.okhttp.MediaType, java.io.File) */ /* package */ static RequestBody create(final MediaType contentType, final File file, final long startOffset, final ProgressListener listener) { if (file == null) { throw new NullPointerException("content == null"); } if (listener == null && startOffset == 0) { return RequestBody.create(contentType, file); } return new RequestBody() { private void updateProgress(long loaded) throws CancelledUploadingException { if (listener != null) { if (listener.hasCancelled()) { throw new CancelledUploadingException(); } listener.updateProgress(loaded + startOffset, file.length()); } } @Override public MediaType contentType() { return contentType; } @Override public long contentLength() { return file.length() - startOffset; } @Override public void writeTo(BufferedSink sink) throws IOException { Source source = null; InputStream inputStream = new FileInputStream(file); try { if (startOffset > 0) { long skipped = inputStream.skip(startOffset); if (skipped != startOffset) { throw new IOException("RequestBodyProgress: inputStream.skip() failed"); } } long loaded = 0; updateProgress(loaded); source = Okio.source(inputStream); Buffer buffer = new Buffer(); for (long readCount; (readCount = source.read(buffer, SIZE)) != -1; ) { sink.write(buffer, readCount); loaded += readCount; updateProgress(loaded); } logger.debug("loaded: " + loaded); } finally { Util.closeQuietly(source); Util.closeQuietly(inputStream); } } }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy