/*
Upload parts of the file. The part size is retrieved from the upload session.
516190
private List<BoxFileUploadSessionPart> uploadParts(BoxFileUploadSession.Info session, InputStream stream,
                                                       long fileSize) throws InterruptedException {
        List<BoxFileUploadSessionPart> parts = new ArrayList<BoxFileUploadSessionPart>();

        int partSize = session.getPartSize();
        long offset = 0;
        long processed = 0;
        int partPostion = 0;
        //Set the Max Queue Size to 1.5x the number of processors
        double maxQueueSizeDouble = Math.ceil(this.executorService.getMaximumPoolSize() * 1.5);
        int maxQueueSize = Double.valueOf(maxQueueSizeDouble).intValue();
        while (processed < fileSize) {
            //Waiting for any thread to finish before
            long timeoutForWaitingInMillis = TimeUnit.MILLISECONDS.convert(this.timeout, this.timeUnit);
            if (this.executorService.getCorePoolSize() <= this.executorService.getActiveCount()) {
                if (timeoutForWaitingInMillis > 0) {
                    Thread.sleep(LargeFileUpload.THREAD_POOL_WAIT_TIME_IN_MILLIS);
                    timeoutForWaitingInMillis -= THREAD_POOL_WAIT_TIME_IN_MILLIS;
                } else {
                    throw new BoxAPIException("Upload parts timedout");
                }
            }
            if (this.executorService.getQueue().size() < maxQueueSize) {
                long diff = fileSize - (long) processed;
                //The size last part of the file can be lesser than the part size.
                if (diff < (long) partSize) {
                    partSize = (int) diff;
                }
                parts.add(null);
                byte[] bytes = new byte[partSize];
                try {
                    int readStatus = stream.read(bytes);
                    if (readStatus == -1) {
                        throw new BoxAPIException("Stream ended while upload was progressing");
                    }
                } catch (IOException ioe) {
                    throw new BoxAPIException("Reading data from stream failed.", ioe);
                }
                this.executorService.execute(
                    new LargeFileUploadTask(session.getResource(), bytes, offset,
                        partSize, fileSize, parts, partPostion)
                );

                //Increase the offset and proceesed bytes to calculate the Content-Range header.
                processed += partSize;
                offset += partSize;
                partPostion++;
            }
        }
        this.executorService.shutdown();
        this.executorService.awaitTermination(this.timeout, this.timeUnit);
        return parts;
    }
708852
public OkRequest<T> part(final String name, final String filename,
                             final String contentType, final InputStream part) throws IOException {

        try {
            startPart();
            writePartHeader(name, filename, contentType);
            copy(part, mOutput);
        } catch (IOException ex) {
            throw ex;
        } catch (Exception ex) {
            VolleyLog.e(ex, "error on part");
        }


        return this;
    }
140625
private FSDataOutputStream createNewPartStream(Path dst, int partId) throws IOException {
    String partName = PART_PREFIX + partId;
    Path output = new Path(dst, partName);
    FileSystem destFs = output.getFileSystem(conf);
    FSDataOutputStream partStream = destFs.create(output, false,
        conf.getInt("io.file.buffer.size", 4096), destFs.getDefaultReplication(),
        conf.getLong(HAR_BLOCKSIZE_LABEL, HAR_BLOCKSIZE_DEFAULT));
    return partStream;
  }
355247
public void addFilePart(final String fieldName, final URL urlToUploadFile)
        throws IOException
    {
        //
        // Maybe try and extract a filename from the last part of the url?
        // Or have the user pass it in?
        // Or just leave it blank as I have already done?
        //
        addFilePart(fieldName,
                    urlToUploadFile.openStream(),
                    null,
                    URLConnection.guessContentTypeFromName(urlToUploadFile.toString()));
    }
620767
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {

        final List<PartETag> partETags = new ArrayList<PartETag>();

        while (requestFactory.hasMoreRequests()) {
            if (threadPool.isShutdown()) throw new CancellationException("TransferManager has been shutdown");
            UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
            // Mark the stream in case we need to reset it
            InputStream inputStream = uploadPartRequest.getInputStream();
            if (inputStream != null && inputStream.markSupported()) {
                if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                    inputStream.mark(Integer.MAX_VALUE);
                } else {
                    inputStream.mark((int)uploadPartRequest.getPartSize());
                }
            }
            partETags.add(s3.uploadPart(uploadPartRequest).getPartETag());
        }

        CompleteMultipartUploadRequest req =
            new CompleteMultipartUploadRequest(
                origReq.getBucketName(), origReq.getKey(), multipartUploadId,
                    partETags)
                    .withRequesterPays(origReq.isRequesterPays())
            .withGeneralProgressListener(origReq.getGeneralProgressListener())
            .withRequestMetricCollector(origReq.getRequestMetricCollector())
            ;
        CompleteMultipartUploadResult res = s3.completeMultipartUpload(req);

        UploadResult uploadResult = new UploadResult();
        uploadResult.setBucketName(res.getBucketName());
        uploadResult.setKey(res.getKey());
        uploadResult.setETag(res.getETag());
        uploadResult.setVersionId(res.getVersionId());
        return uploadResult;
    }
1020047
public void put(final File file, final String key, String token, final UpCompletionHandler complete,
                    final UploadOptions options) {
        final UpToken decodedToken = UpToken.parse(token);
        if (areInvalidArg(key, null, file, token, decodedToken, complete)) {
            return;
        }

        Zone z = config.zone;
        z.preQuery(token, new Zone.QueryHandler() {
            @Override
            public void onSuccess() {
                long size = file.length();
                if (size <= config.putThreshold) {
                    FormUploader.upload(client, config, file, key, decodedToken, complete, options);
                    return;
                }
                String recorderKey = config.keyGen.gen(key, file);
                final WarpHandler completionHandler = warpHandler(complete, file != null ? file.length() : 0);
                ResumeUploader uploader = new ResumeUploader(client, config, file, key,
                        decodedToken, completionHandler, options, recorderKey);

                AsyncRun.runInMain(uploader);
            }

            @Override
            public void onFailure(int reason) {
                final ResponseInfo info = ResponseInfo.isStatusCodeForBrokenNetwork(reason) ?
                        ResponseInfo.networkError(reason, decodedToken) :
                        ResponseInfo.invalidToken("invalid token");
                complete.complete(key, info, null);
            }
        });

    }
611445
def _get_total_multipart_upload_size(self, bucket_name, object_name,
                                         upload_id):
        
        return sum(
            [part.size for part in
             self._list_object_parts(bucket_name, object_name, upload_id)]
        )
587517
private static MultipartFormData parseMultipart(UploadSetting uploadSetting) throws IOException {
		MultipartFormData formData = new MultipartFormData(uploadSetting);
		formData.parseRequest(getServletRequest());

		return formData;
	}
1341491
@POST
  @Path("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response uploadFile(
      @FormDataParam("file") InputStream uploadedInputStream,
      @FormDataParam("file") FormDataContentDisposition fileDetail) {

    Config config = createConfig();

    if (uploadedInputStream == null) {
      String msg = "input stream is null";
      LOG.error(msg);
      return Response.status(Response.Status.BAD_REQUEST)
          .type(MediaType.APPLICATION_JSON)
          .entity(Utils.createMessage(msg)).build();
    }
    if (fileDetail == null) {
      String msg = "form data content disposition is null";
      LOG.error(msg);
      return Response.status(Response.Status.BAD_REQUEST)
          .type(MediaType.APPLICATION_JSON)
          .entity(Utils.createMessage(msg)).build();
    }

    String uploadDir = config.getStringValue(FILE_SYSTEM_DIRECTORY);

    final String fileName = UUID.randomUUID() + "-" + fileDetail.getFileName();

    final String uploadedFileLocation
        = uploadDir + "/" + fileName;

    // save it
    try {
      FileHelper.writeToFile(uploadedInputStream, uploadedFileLocation);
    } catch (IOException e) {
      LOG.error("error uploading file {}", fileDetail.getFileName(), e);
      return Response.serverError()
          .type(MediaType.APPLICATION_JSON)
          .entity(Utils.createMessage(e.getMessage()))
          .build();
    }

    String uri = String.format("http://%s:%s/api/v1/file/download/%s",
        getHostNameOrIP(), getPort(), fileName);

    return Response.status(Response.Status.OK).entity(uri).build();
  }
497036
public <T extends UploadObjectRequest> T withUploadPartMetadata(
            ObjectMetadata partUploadMetadata) {
        setUploadPartMetadata(partUploadMetadata);
        @SuppressWarnings("unchecked") T t = (T)this;
        return t;
    }
651966
private ServletRequest init() throws MultipartException, IOException {
		// retrieve multipart/form-data parameters
		if (Multipart.isMultipartContent(request)) {
			Multipart multipart = new Multipart();
			multipart.parse(request, new PartHandler() {

				@Override
				public void handleFormItem(String name, String value) {
					multipartParams.put( name, value );
				}

				@Override
				public void handleFileItem(String name, FileItem fileItem) {
					files.add(fileItem);
				}

			});
		}

		return this;
	}
1291207
public OkRequest<T> part(final String name, final String filename,
                             final File part) throws IOException {
        return part(name, filename, null, part);
    }
1361485
protected UploadPartRequest newUploadPartRequest(PartCreationEvent event,
            final File part) {
        final UploadPartRequest reqUploadPart = new UploadPartRequest()
            .withBucketName(req.getBucketName())
            .withFile(part)
            .withKey(req.getKey())
            .withPartNumber(event.getPartNumber())
            .withPartSize(part.length())
            .withLastPart(event.isLastPart())
            .withUploadId(uploadId)
            .withObjectMetadata(req.getUploadPartMetadata())
            ;
        return reqUploadPart;
    }
471972
function onPart (part) {
  var form = this;

  if (undefined === part.filename) return form.handlePart(part);

  ++form._flushing;

  var file = new File(part.filename, form.__filename(part.filename), part.mime);

  form.emit('fileBegin', part.name, file);

  var options = { mode: 'w', content_type: file.type, filename: file.path };
  if (form.chunk_size) options.chunk_size = form.chunk_size;
  if (form.root) options.root = form.root;
  options.metadata = file.metadata ? file.metadata
                   : form.metadata ? form.metadata
                   : undefined
  var ws = form.__gridstream.createWriteStream(options);

  ws.on('progress', onprogress);
  ws.on('drain', resume);
  ws.once('error', done);
  part.on('data', onData);
  part.once('end', onEnd);

  function onprogress (size) {
    file.lastModified = new Date;
    file.size = size;
    file.emit('progress', size);
  }

  function resume () {
    form.resume();
  };

  function onData (data) {
    form.pause();
    ws.write(data);
  };

  function onEnd () {
    part.removeListener('data', onData);
    part.removeListener('end', onEnd);
    ws.once('close', function (_) {
      done();
    });
    ws.end();
  };

  function done (err) {
    if (done.err) return;
    if (err) return form.emit('error', done.err = err);

    ws.removeListener('progress', onprogress);
    ws.removeListener('drain', resume);
    ws.removeListener('error', done);

    file.id || (file.id = ws._store.fileId);
    file.root = ws._store.root;
    file.size = ws._store.position;
    file.emit('end');

    --form._flushing;

    form.emit('file', part.name, file);
    form._maybeEnd();
  }
}
613353
def validate_partial_uploads!(part_uids)
      input = Queue.new
      part_uids.each { |part_uid| input << part_uid }
      input.close

      results = Queue.new

      thread_count   = storage.concurrency[:concatenation] if storage.respond_to?(:concurrency)
      thread_count ||= 10

      threads = thread_count.times.map do
        Thread.new do
          begin
            loop do
              part_uid = input.pop or break
              part_info = storage.read_info(part_uid)
              results << Tus::Info.new(part_info)
            end
            nil
          rescue => error
            input.clear
            error
          end
        end
      end

      errors = threads.map(&:value).compact

      if errors.any? { |error| error.is_a?(Tus::NotFound) }
        error!(400, "One or more partial uploads were not found")
      elsif errors.any?
        fail errors.first
      end

      part_infos = Array.new(results.size) { results.pop } # convert Queue into an Array

      unless part_infos.all?(&:partial?)
        error!(400, "One or more uploads were not partial")
      end

      if max_size && part_infos.map(&:length).inject(0, :+) > max_size
        error!(400, "The sum of partial upload lengths exceed Tus-Max-Size")
      end
    end
1105149
public String uploadFile(UploadFileRequest request) throws AbstractCosException {
		request.check_param();

		String localPath = request.getLocalPath();
		long fileSize = 0;
		try {
			fileSize = CommonFileUtils.getFileLength(localPath);
		} catch (Exception e) {
			throw new UnknownException(e.toString());
		}

		long suitSingleFileSize = 8 * 1024 * 1024;
		if (fileSize < suitSingleFileSize) {
			return uploadSingleFile(request);
		} else {
			UploadSliceFileRequest sliceRequest = new UploadSliceFileRequest(request);
			sliceRequest.setInsertOnly(request.getInsertOnly());
			return uploadSliceFile(sliceRequest);
		}
	}
76599
public UploadPartResult uploadPart(UploadPartRequest request)
      throws AmazonClientException {
    try {
      UploadPartResult uploadPartResult = mClient.uploadPart(request);
      return uploadPartResult;
    } catch (AmazonClientException e) {
      throw e;
    }
  }