1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """
16 This module adds the buffer support to the python vert.x platform
17 """
18
19 import org.vertx.java.core.buffer.Buffer
20
21 __author__ = "Scott Horn"
22 __email__ = "scott@hornmicro.com"
23 __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
26 """A Buffer represents a sequence of zero or more bytes that can be written to or read from, and which expands
27 as necessary to accomodate any bytes written to it.
28
29 Buffers are used in many places in vert.x, for example to read/write data to/from NetSocket, AsyncFile,
30 WebSocket, HttpClientRequest, HttpClientResponse, HttpServerRequest, HttpServerResponse etc.
31
32 There are two ways to write data to a Buffer: The first method involves methods that take the form set_XXX.
33 These methods write data into the buffer starting at the specified position. The position does not have to be inside data that
34 has already been written to the buffer; the buffer will automatically expand to encompass the position plus any data that needs
35 to be written. All positions are measured in bytes and start with zero.
36
37 The second method involves methods that take the form append-XXX; these methods append data at the end of the buffer.
38 Methods exist to both set and append all primitive types, String and other instances of Buffer.
39
40 Data can be read from a buffer by invoking methods which take the form get_XXX. These methods take a parameter
41 representing the position in the Buffer from where to read data.
42 """
45
47 """ String representation of buffer """
48 return self.buffer.toString("UTF-8")
49
51 """ Buffer in enc encoding """
52 return self.buffer.toString(enc)
53
54 @staticmethod
58
59 @staticmethod
60 - def create(initial_size_hint = 0):
61 """ Creates a new empty buffer. initial_size_hint is a hint to the system for how much memory to initially allocate."""
62 return Buffer(org.vertx.java.core.buffer.Buffer(initial_size_hint))
63
65 """ Get the byte at position pos in the buffer. """
66 return self.buffer.getByte(pos)
67
69 """ Get the fixnum represented by a sequence of bytes starting at position pos in the buffer. """
70 if bytes == 1:
71 return self.buffer.getByte(pos)
72 elif bytes == 2:
73 return self.buffer.getShort(pos)
74 elif bytes == 4:
75 return self.buffer.getInt(pos)
76 elif bytes == 8:
77 return self.buffer.getLong(pos)
78 else:
79 raise Exception("bytes must be 1, 2, 4, or 8")
80
82 """ Get the float represented by a sequence of bytes starting at position pos in the buffer. """
83 if bytes == 4:
84 return self.buffer.getFloat(pos)
85 elif bytes == 8:
86 return self.buffer.getDouble(pos)
87 else:
88 raise Exception("bytes must be 4 or 8")
89
91 """ Return bytes from the buffer interpreted as a String """
92 return self.buffer.getString(pos, end_pos, enc)
93
95 """ Return bytes in the buffer as a Buffer """
96 return Buffer(self.buffer.getBuffer(pos, end_pos))
97
99 """ Appends a buffer to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. """
100 self.buffer.appendBuffer(buff._to_java_buffer())
101 return self
102
104 """ Appends a fixnum to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. """
105 if bytes == 1:
106 self.buffer.appendByte(num)
107 elif bytes == 2:
108 self.buffer.appendShort(num)
109 elif bytes == 4:
110 self.buffer.appendInt(num)
111 elif bytes == 8:
112 self.buffer.appendLong(num)
113 else:
114 raise Exception("bytes must be 1, 2, 4, or 8")
115 return self
116
118 """ Appends a float to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. """
119 if bytes == 4:
120 self.buffer.appendFloat(num)
121 elif bytes == 8:
122 self.buffer.appendDouble(num)
123 else:
124 raise Exception("bytes must be 4 or 8")
125
127 """ Appends a string to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. """
128 self.buffer.appendString(str, enc)
129 return self
130
132 """ Sets bytes in the buffer to a representation of a fixnum. The buffer will expand as necessary to accomodate any bytes written. """
133 if bytes == 1:
134 self.buffer.setByte(pos, num)
135 elif bytes == 2:
136 self.buffer.setShort(pos, num)
137 elif bytes == 4:
138 self.buffer.setInt(pos, num)
139 elif bytes == 8:
140 self.buffer.setLong(pos, num)
141 else:
142 raise Exception("bytes must be 1, 2, 4, or 8")
143 return self
144
146 """ Sets bytes in the buffer to a representation of a float. The buffer will expand as necessary to accomodate any bytes written. """
147 if bytes == 4:
148 self.buffer.setFloat(pos, num)
149 elif bytes == 8:
150 self.buffer.setDouble(pos, num)
151 else:
152 raise Exception("bytes must be 4 or 8")
153 return self
154
156 """ Sets bytes in this buffer to the bytes of the specified buffer. The buffer will expand as necessary to accomodate any bytes written. """
157 self.buffer.setBytes(pos, buff._to_java_buffer)
158 return self
159
161 """ Set bytes in the buffer to the string encoding in the specified encoding """
162 self.buffer.setString(pos, str, enc)
163 return self
164
165 @property
167 """ The length of this buffer, in bytes. """
168 return self.buffer.length()
169
171 """ Get a copy of the entire buffer. """
172 return Buffer(self.buffer.copy())
173
175 """ private """
176 return self.buffer
177