Class: RabbitFileSplit::Bytes

Inherits:
Object
  • Object
show all
Includes:
ByteFormat
Defined in:
lib/rabbit_file_split/bytes.rb

Constant Summary collapse

BUFFER_SIZE =

GNU utils split.c bytes_split interface

bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize, size_t initial_read, uintmax_t max_files)
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ByteFormat

#to_byte

Constructor Details

#initialize(file_path) ⇒ Bytes

Returns RabbitFIleSplit::Bytes instance.

Parameters:

  • file_path (String)

    target file path



18
19
20
21
22
23
# File 'lib/rabbit_file_split/bytes.rb', line 18

def initialize(file_path)
  @file_path = file_path
  @file_base_name = File::basename(file_path)
  @file_parent_path = File::dirname(file_path)
  @original_file_size = File.size(file_path)
end

Instance Attribute Details

#file_base_nameObject

Returns the value of attribute file_base_name.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def file_base_name
  @file_base_name
end

#file_listObject

Returns the value of attribute file_list.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def file_list
  @file_list
end

#file_parent_pathObject

Returns the value of attribute file_parent_path.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def file_parent_path
  @file_parent_path
end

#file_pathObject

Returns the value of attribute file_path.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def file_path
  @file_path
end

#original_file_sizeObject

Returns the value of attribute original_file_size.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def original_file_size
  @original_file_size
end

#split_file_numObject

Returns the value of attribute split_file_num.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def split_file_num
  @split_file_num
end

#split_work_timeObject

Returns the value of attribute split_work_time.



11
12
13
# File 'lib/rabbit_file_split/bytes.rb', line 11

def split_work_time
  @split_work_time
end

Instance Method Details

#delete_allInteger

Returns delete file num.

Returns:

  • (Integer)

    delete file num



89
90
91
# File 'lib/rabbit_file_split/bytes.rb', line 89

def delete_all
  File.delete(*@file_list)
end

#split(n_bytes, file_prefix: @file_base_name, dest_file_path: @file_parent_path, buffer_size: BUFFER_SIZE) ⇒ Object

Returns void.

Parameters:

  • n_bytes (String)

    base byte num or N + byte unit

  • file_prefix (String) (defaults to: @file_base_name)

    file_prefix. default is original file name

  • dest_file_path (String) (defaults to: @file_parent_path)

    generate split file directory

  • buffer_size (String) (defaults to: BUFFER_SIZE)

    file buffer size

Returns:

  • void



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rabbit_file_split/bytes.rb', line 30

def split(n_bytes, file_prefix: @file_base_name, dest_file_path: @file_parent_path, buffer_size: BUFFER_SIZE)
  start_time = Time.now
  n_bytes = to_byte(n_bytes)
  total_readed_byte = 0
  unit_readed_byte = 0
  @split_file_num  = 1
  io = File.open(@file_path, 'rb')
  w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
  while(true)
    buffer = io.read(buffer_size)
    #puts buffer_size
    total_readed_byte += buffer_size
    unit_readed_byte += buffer_size
    w_file_io.write(buffer)

    #check total readed
    if (total_readed_byte + buffer_size) > @original_file_size
      total_rest_byte = @original_file_size - total_readed_byte
      if ( (unit_readed_byte + total_rest_byte) > n_bytes )
        # n_bytes over. file change
        rest_byte = n_bytes - unit_readed_byte
        if rest_byte > 0
          rest_buffer = io.read(rest_byte)
          w_file_io.write(rest_buffer)
        end
        w_file_io.close
        @split_file_num +=1
        #file change
        w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
      end
      # read rest byte
      if total_rest_byte > 0
        rest_buffer = io.read(total_rest_byte)
        w_file_io.write(rest_buffer)
      end

      end_time = Time.now
      @split_work_time = end_time - start_time
      w_file_io.close
      return @file_list
    end

    #check n_bytes
    if (unit_readed_byte + buffer_size) > n_bytes
      rest_byte = n_bytes - unit_readed_byte
      if rest_byte > 0
        rest_buffer = io.read(rest_byte)
        w_file_io.write(rest_buffer)
      end
      w_file_io.close
      @split_file_num +=1
      #file change
      unit_readed_byte = 0
      w_file_io = File.open(create_split_file_path(dest_file_path,file_prefix,@split_file_num), 'wb')
    end
  end
end