Sha256: 3dbe335cd6eab0f8966526ff5edeb5d86885fa86d60591e7341dc0fdadc6804b

Contents?: true

Size: 1.74 KB

Versions: 1030

Compression:

Stored size: 1.74 KB

Contents

module Aws
  module S3

    # A utility class that provides an IO-like interface to a portion of
    # a file on disk.
    # @api private
    class FilePart

      # @option options [required,String,Pathname,File,Tempfile] :source
      # @option options [required,Integer] :offset The file part will read
      #   starting at this byte offset.
      # @option options [required,Integer] :size The maximum number of bytes to
      #   read from the `:offset`.
      def initialize(options = {})
        @source = options[:source]
        @first_byte = options[:offset]
        @last_byte = @first_byte + options[:size]
        @size = options[:size]
        @file = nil
      end

      # @return [String,Pathname,File,Tempfile]
      attr_reader :source

      # @return [Integer]
      attr_reader :first_byte

      # @return [Integer]
      attr_reader :last_byte

      # @return [Integer]
      attr_reader :size

      def read(bytes = nil, output_buffer = nil)
        open_file unless @file
        read_from_file(bytes, output_buffer)
      end

      def rewind
        if @file
          @file.seek(@first_byte)
          @position = @first_byte
        end
        0
      end

      def close
        @file.close if @file
      end

      private

      def open_file
        @file = File.open(@source, 'rb')
        rewind
      end

      def read_from_file(bytes, output_buffer)
        if bytes
          data = @file.read([remaining_bytes, bytes].min)
          data = nil if data == ''
        else
          data = @file.read(remaining_bytes)
        end
        @position += data ? data.bytesize : 0
        output_buffer ? output_buffer.replace(data || '') : data
      end

      def remaining_bytes
        @last_byte - @position
      end

    end
  end
end

Version data entries

1,030 entries across 1,030 versions & 3 rubygems

Version Path
aws-sdk-resources-2.2.14 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.13 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.12 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.11 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.10 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.9 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.8 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.7 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.6 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.5 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.4 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.3 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.2 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.1 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.2.0 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.1.36 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.1.35 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.1.34 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.1.33 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.1.32 lib/aws-sdk-resources/services/s3/file_part.rb