Class: ChunkWriterIO

Inherits:
StringIO
  • Object
show all
Defined in:
lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb

Constant Summary collapse

MAX_CHUNK_SIZE =

Writer for chunked data.

0xFFFF

Instance Method Summary collapse

Constructor Details

#initializeChunkWriterIO

Returns a new instance of ChunkWriterIO.



8
9
10
11
12
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 8

def initialize
  @output_buffer = []
  @output_size = 0
  super
end

Instance Method Details

#buffer_result(zero_chunk = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 55

def buffer_result(zero_chunk = false)
  result = ''

  if !@output_buffer.empty?
    result << [@output_size].pack('s>*')
    result.concat(@output_buffer.join)
  end

  result << "\x00\x00" if zero_chunk

  result
end

#clear_buffer!Object



68
69
70
71
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 68

def clear_buffer!
  @output_buffer.clear
  @output_size = 0
end

#close(zero_chunk = false) ⇒ Object

Close the stream.



44
45
46
47
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 44

def close(zero_chunk = false)
  flush(zero_chunk)
  super
end

#flush(zero_chunk = false) ⇒ Object



36
37
38
39
40
41
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 36

def flush(zero_chunk = false)
  write_without_chunking(buffer_result(zero_chunk))
  clear_buffer!

  super()
end

#write_buffer!(string, size) ⇒ Object

private



50
51
52
53
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 50

def write_buffer!(string, size)
  @output_buffer << string
  @output_size = size
end

#write_with_chunking(string) ⇒ Object Also known as: write

Write some bytes, splitting into chunks if necessary.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb', line 15

def write_with_chunking(string)
  until string.empty?
    future_size = @output_size + string.size
    if future_size >= MAX_CHUNK_SIZE
      last = MAX_CHUNK_SIZE - @output_size
      write_buffer!(string[0, last], MAX_CHUNK_SIZE)
      string = string[last..-1]

      write_without_chunking(buffer_result)
      clear_buffer!
    else
      write_buffer!(string, future_size)

      string = ''
    end
  end
end