Computes the actual bytes to be sent in a part, which equals the partsize for all parts
except the last.
@param uploadSession
@param partNumber
@param fileSize
@return
342834
public static int getChunkSize(BoxUploadSession uploadSession, int partNumber, long fileSize) {
        if (partNumber == uploadSession.getTotalParts() - 1) {
            return (int) (fileSize - partNumber * uploadSession.getPartSize());
        }
        return uploadSession.getPartSize();
    }
198755
public void addFilePart(final String fieldName, final File uploadFile)
        throws IOException
    {
        String fileName = uploadFile.getName();

        addFilePart(fieldName, new FileInputStream(uploadFile), fileName, URLConnection.guessContentTypeFromName(fileName));
    }
420844
private UploadResult uploadInParts() throws Exception {
        boolean isUsingEncryption = s3 instanceof AmazonS3Encryption;
        long optimalPartSize = getOptimalPartSize(isUsingEncryption);

        try {
            if (multipartUploadId == null) {
                multipartUploadId = initiateMultipartUpload(origReq,
                        isUsingEncryption);
            }

            UploadPartRequestFactory requestFactory = new UploadPartRequestFactory(origReq, multipartUploadId, optimalPartSize);

            if (TransferManagerUtils.isUploadParallelizable(origReq, isUsingEncryption)) {
                captureUploadStateIfPossible();
                uploadPartsInParallel(requestFactory, multipartUploadId);
                return null;
            } else {
                return uploadPartsInSeries(requestFactory);
            }
        } catch (Exception e) {
            publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
            performAbortMultipartUpload();
            throw e;
        } finally {
            if (origReq.getInputStream() != null) {
                try {origReq.getInputStream().close(); } catch (Exception e) {
                    log.warn("Unable to cleanly close input stream: " + e.getMessage(), e);
                }
            }
        }
    }
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;
    }
1034797
function (callback) {
    var partBuffer = preparePartBuffer();

    var localPartNumber = partNumber;
    partNumber++;
    receivedSize += partBuffer.length;
    cachedClient.uploadPart(
      {
        Body: partBuffer,
        Bucket: destinationDetails.Bucket,
        Key: destinationDetails.Key,
        UploadId: multipartUploadID,
        PartNumber: localPartNumber
      },
      function (err, result) {
        if (err)
          abortUpload('Failed to upload a part to S3: ' + JSON.stringify(err));
        else {
          uploadedSize += partBuffer.length;
          partIds[localPartNumber - 1] = {
            ETag: result.ETag,
            PartNumber: localPartNumber
          };

          callback({
            ETag: result.ETag,
            PartNumber: localPartNumber,
            receivedSize: receivedSize,
            uploadedSize: uploadedSize
          });
        }
      }
    );
  }
610964
def _upload_in_splits( self, destination_folder_id, source_path, preflight_check, verbose = True, chunked_upload_threads = 5 ):
        '''
        
        '''
        file_size = os.stat(source_path).st_size
        split_size = BOX_MAX_FILE_SIZE

        # Make sure that the last split piece is still big enough for a chunked upload
        while file_size % split_size < BOX_MIN_CHUNK_UPLOAD_SIZE:
            split_size -= 1000
            if split_size < BOX_MIN_CHUNK_UPLOAD_SIZE:
                raise Exception('Lazy programming error')

        split_start_byte = 0
        part_count = 0
        uploaded_file_ids = []
        while split_start_byte < file_size:
            dest_file_name = '{0}.part{1}'.format( os.path.basename(source_path), part_count)
            prev_uploaded_file_ids = self.find_file( destination_folder_id, dest_file_name )
            if len( prev_uploaded_file_ids ) == 1:
                if verbose:
                    print ( '\nSkipping upload of split {0} of {1}; already exists'.format( part_count + 1, math.ceil(file_size / split_size) ) )
                uploaded_file_ids.extend( prev_uploaded_file_ids )
            else:
                if verbose:
                    print ( '\nUploading split {0} of {1}'.format( part_count + 1, math.ceil(file_size / split_size) ) )
                uploaded_file_ids.append( self._chunked_upload(
                    destination_folder_id, source_path,
                    dest_file_name = dest_file_name,
                    split_start_byte = split_start_byte,
                    file_size = min(split_size, file_size - split_start_byte), # Take the min of file_size - split_start_byte so that the last part of a split doesn't read into the next split
                    preflight_check = preflight_check,
                    verbose = verbose,
                    upload_threads = chunked_upload_threads,
                ) )
            part_count += 1
            split_start_byte += split_size

        return uploaded_file_ids
241958
public static Part<byte[]> file(String name, String fileName, byte[] bytes) {
        return new Part<>(name, fileName, bytes, CONTENT_TYPE_BINARY, null, (body, out, charset) -> out.write(body));
    }
983091
public int allocateSpace(final int iSize) throws IOException {
    final int offset = getFilledUpTo();
    final int size = getFileSize();

    if (getFreeSpace() < iSize) {
      if (maxSize > 0 && maxSize - size < iSize)
        throw new IllegalArgumentException("Cannot enlarge file since the configured max size ("
            + OFileUtils.getSizeAsString(maxSize) + ") was reached! " + toString());

      // MAKE ROOM
      int newFileSize = size;

      if (newFileSize == 0)
        // PROBABLY HAS BEEN LOST WITH HARD KILLS
        newFileSize = DEFAULT_SIZE;

      // GET THE STEP SIZE IN BYTES
      int stepSizeInBytes = incrementSize > 0 ? incrementSize : -1 * size / 100 * incrementSize;

      // FIND THE BEST SIZE TO ALLOCATE (BASED ON INCREMENT-SIZE)
      while (newFileSize - offset <= iSize) {
        newFileSize += stepSizeInBytes;

        if (newFileSize == 0)
          // EMPTY FILE: ALLOCATE REQUESTED SIZE ONLY
          newFileSize = iSize;
        if (newFileSize > maxSize && maxSize > 0)
          // TOO BIG: ROUND TO THE MAXIMUM FILE SIZE
          newFileSize = maxSize;
      }

      setSize(newFileSize);
    }

    // THERE IS SPACE IN FILE: RETURN THE UPPER BOUND OFFSET AND UPDATE THE FILLED THRESHOLD
    setFilledUpTo(offset + iSize);

    return offset;
  }
180159
private String getCommitBody(List<BoxFileUploadSessionPart> parts, Map<String, String> attributes) {
        JsonObject jsonObject = new JsonObject();

        JsonArray array = new JsonArray();
        for (BoxFileUploadSessionPart part: parts) {
            JsonObject partObj = new JsonObject();
            partObj.add("part_id", part.getPartId());
            partObj.add("offset", part.getOffset());
            partObj.add("size", part.getSize());

            array.add(partObj);
        }
        jsonObject.add("parts", array);

        if (attributes != null) {
            JsonObject attrObj = new JsonObject();
            for (String key: attributes.keySet()) {
                attrObj.add(key, attributes.get(key));
            }
            jsonObject.add("attributes", attrObj);
        }

        return jsonObject.toString();
    }
1214447
private void getPartList(WebContext ctx, Bucket bucket, String id, String uploadId) {
        if (!multipartUploads.contains(uploadId)) {
            ctx.respondWith().error(HttpResponseStatus.NOT_FOUND, ERROR_MULTIPART_UPLOAD_DOES_NOT_EXIST);
            return;
        }

        Response response = ctx.respondWith();

        response.setHeader(HTTP_HEADER_NAME_CONTENT_TYPE, CONTENT_TYPE_XML);

        XMLStructuredOutput out = response.xml();
        out.beginOutput("ListPartsResult");
        out.property(RESPONSE_BUCKET, bucket.getName());
        out.property("Key", id);
        out.property("UploadId", uploadId);

        outputOwnerInfo(out, "Initiator");
        outputOwnerInfo(out, "Owner");

        File uploadDir = getUploadDir(uploadId);
        int marker = ctx.get("part-number-marker").asInt(0);
        int maxParts = ctx.get("max-parts").asInt(0);

        out.property("StorageClass", "STANDARD");
        out.property("PartNumberMarker", marker);
        if ((marker + maxParts) < uploadDir.list().length) {
            out.property("NextPartNumberMarker", marker + maxParts + 1);
        }

        if (Strings.isFilled(maxParts)) {
            out.property("MaxParts", maxParts);
        }

        boolean truncated = 0 < maxParts && maxParts < uploadDir.list().length;
        out.property("IsTruncated", truncated);

        for (File part : uploadDir.listFiles()) {
            out.beginObject("Part");
            out.property("PartNumber", part.getName());
            out.property("LastModified", RFC822_INSTANT.format(Instant.ofEpochMilli(part.lastModified())));
            try {
                out.property(HTTP_HEADER_NAME_ETAG, Files.hash(part, Hashing.md5()).toString());
            } catch (IOException e) {
                Exceptions.ignore(e);
            }
            out.property("Size", part.length());
            out.endObject();
        }

        out.endOutput();
    }
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;
  }
923893
@Override
    public long getContentSize(Context ctx) throws StreamIOException {
        try {
            // For new or modified datastreams, the new bytestream hasn't yet been
            // committed. However, we need to access it in order to compute
            // the datastream checksum
            if (DSLocation.startsWith(UPLOADED_SCHEME)) {
                // TODO: refactor to use proper temp file management - FCREPO-718
                // for now, just get the file directly (see also DefaultManagement.getTempStream(...))
                String internalId = DSLocation.substring(UPLOADED_SCHEME.length());
                File uploadedFile = new File(getTempUploadDir(), internalId);
                // check it has not been automatically purged (see DefaultManagement.purgeUploadedFiles())
                if (uploadedFile.exists()) {
                    return uploadedFile.length();
                } else {
                    throw new StreamIOException("Uploaded file " + DSLocation + " no longer exists.");
                }

            } else if (DSLocation.startsWith(TEMP_SCHEME)) {
                // TODO: refactor to use proper temp file management - FCREPO-718
                String fileName = DSLocation.substring(TEMP_SCHEME.length());
                File tempFile = new File(fileName);
                // check it has not been removed elsewhere (should not happen)
                if (tempFile.exists()) {
                    return tempFile.length();
                } else {
                    throw new StreamIOException("Temp file " + DSLocation + " no longer exists.");
                }
            } else if (Datastream.DS_LOCATION_TYPE_INTERNAL.equals(DSLocationType)) {
                ILowlevelStorage llstore = getLLStore();
                if (s_llsizable) {
                    return ((ISizable)llstore).getDatastreamSize(DSLocation);
                } else {
                    return 0;
                }
            } else if (Datastream.DS_LOCATION_TYPE_URL.equals(DSLocationType)) {
                // Managed content can have URL dsLocation on ingest
                ValidationUtility.validateURL(DSLocation, this.DSControlGrp);
                // If validation has succeeded, assume an external resource.
                // Fetch it, store it locally, update DSLocation
                if (ctx == null) {
                    ctx = ReadOnlyContext.getContext(null, null, "", false);
                    // changed from below, which sets noOp to true, seems to cause an AuthZ permitted exception
                    // in PolicyEnforcementPoint.enforce(...)
                    // ctx = ReadOnlyContext.EMPTY;
                }
               ContentManagerParams params = new ContentManagerParams(DSLocation);
                params.setContext(ctx);
                MIMETypedStream stream = getExternalContentManager()
                        .getExternalContent(params);

                // TODO: refactor temp file management - see FCREPO-718; for now create temp file and write to it
                // note - don't use temp upload directory, use (container's) temp dir (upload dir is for uploads)
                File tempFile = File.createTempFile("managedcontentupdate", null);
                OutputStream os = new FileOutputStream(tempFile);
                StreamUtility.pipeStream(stream.getStream(), os, 32768);
                DSLocation = TEMP_SCHEME + tempFile.getAbsolutePath();
                return tempFile.length();
            }
        } catch (Throwable th) {
            throw new StreamIOException("[DatastreamManagedContent] returned "
                    + " the error: \"" + th.getClass().getName()
                    + "\". Reason: " + th.getMessage(), th);
        }
        throw new StreamIOException("[DatastreamManagedContent] could not resolve dsLocation " + DSLocation + " dsLocationType " + DSLocationType);
    }
1127934
public OkRequest<T> part(final String name, final String filename,
                             final String contentType, final File part) throws IOException {
        final InputStream stream;
        stream = new BufferedInputStream(new FileInputStream(part));
//            incrementTotalSize(part.length());

        return part(name, filename, contentType, stream);
    }
386956
private function computePartSize($partSize)
    {
        $partSize = (integer)$partSize;
        if ($partSize <= self::OSS_MIN_PART_SIZE) {
            $partSize = self::OSS_MIN_PART_SIZE;
        } elseif ($partSize > self::OSS_MAX_PART_SIZE) {
            $partSize = self::OSS_MAX_PART_SIZE;
        }
        return $partSize;
    }
1211073
@Nonnull
  public FineUploader5Chunking setParamNamePartByteOffset (@Nonnull @Nonempty final String sParamNamePartByteOffset)
  {
    ValueEnforcer.notEmpty (sParamNamePartByteOffset, "ParamNamePartByteOffset");
    m_sChunkingParamNamesPartByteOffset = sParamNamePartByteOffset;
    return this;
  }
429867
@Nonnull
  public FineUploader5Chunking setPartSize (@Nonnegative final long nPartSize)
  {
    ValueEnforcer.isGT0 (nPartSize, "PartSize");
    m_nChunkingPartSize = nPartSize;
    return this;
  }
1034530
public int getPartition(K key, V value, 
                          int numPartitions) {
    Integer result = cache.get();
    if (result == null) {
      return part.getPartition(key, value, numPartitions);
    } else {
      return result;
    }
  }