@@ -109,104 +109,3 @@ func init() {
109
109
gob .Register (& memPool {})
110
110
gob .Register (& filePool {})
111
111
}
112
-
113
- // PoolAt provides a way to Allocate and Release BufferAt objects
114
- // PoolAt's mut be concurrent-safe for calls to Get() and Put().
115
- type PoolAt interface {
116
- Get () (BufferAt , error ) // Allocate a BufferAt
117
- Put (buf BufferAt ) error // Release or Reuse a BufferAt
118
- }
119
-
120
- type poolAt struct {
121
- poolAt sync.Pool
122
- }
123
-
124
- // NewPoolAt returns a PoolAt(), it's backed by a sync.Pool so its safe for concurrent use.
125
- // Get() and Put() errors will always be nil.
126
- // It will not work with gob.
127
- func NewPoolAt (New func () BufferAt ) PoolAt {
128
- return & poolAt {
129
- poolAt : sync.Pool {
130
- New : func () interface {} {
131
- return New ()
132
- },
133
- },
134
- }
135
- }
136
-
137
- func (p * poolAt ) Get () (BufferAt , error ) {
138
- return p .poolAt .Get ().(BufferAt ), nil
139
- }
140
-
141
- func (p * poolAt ) Put (buf BufferAt ) error {
142
- buf .Reset ()
143
- p .poolAt .Put (buf )
144
- return nil
145
- }
146
-
147
- type memPoolAt struct {
148
- N int64
149
- PoolAt
150
- }
151
-
152
- // NewMemPoolAt returns a PoolAt, Get() returns an in memory buffer of max size N.
153
- // Put() returns the buffer to the pool after resetting it.
154
- // Get() and Put() errors will always be nil.
155
- func NewMemPoolAt (N int64 ) PoolAt {
156
- return & memPoolAt {
157
- N : N ,
158
- PoolAt : NewPoolAt (func () BufferAt {
159
- return New (N )
160
- }),
161
- }
162
- }
163
-
164
- func (m * memPoolAt ) MarshalBinary () ([]byte , error ) {
165
- buf := bytes .NewBuffer (nil )
166
- err := binary .Write (buf , binary .LittleEndian , m .N )
167
- return buf .Bytes (), err
168
- }
169
-
170
- func (m * memPoolAt ) UnmarshalBinary (data []byte ) error {
171
- buf := bytes .NewReader (data )
172
- err := binary .Read (buf , binary .LittleEndian , & m .N )
173
- m .PoolAt = NewPoolAt (func () BufferAt {
174
- return New (m .N )
175
- })
176
- return err
177
- }
178
-
179
- type filePoolAt struct {
180
- N int64
181
- Directory string
182
- }
183
-
184
- // NewFilePoolAt returns a PoolAt, Get() returns a file-based buffer of max size N.
185
- // Put() closes and deletes the underlying file for the buffer.
186
- // Get() may return an error if it fails to create a file for the buffer.
187
- // Put() may return an error if it fails to delete the file.
188
- func NewFilePoolAt (N int64 , dir string ) PoolAt {
189
- return & filePoolAt {N : N , Directory : dir }
190
- }
191
-
192
- func (p * filePoolAt ) Get () (BufferAt , error ) {
193
- file , err := ioutil .TempFile (p .Directory , "buffer" )
194
- if err != nil {
195
- return nil , err
196
- }
197
- return NewFile (p .N , file ), nil
198
- }
199
-
200
- func (p * filePoolAt ) Put (buf BufferAt ) (err error ) {
201
- buf .Reset ()
202
- if fileBuf , ok := buf .(* fileBuffer ); ok {
203
- fileBuf .file .Close ()
204
- err = os .Remove (fileBuf .file .Name ())
205
- }
206
- return err
207
- }
208
-
209
- func init () {
210
- gob .Register (& memPoolAt {})
211
- gob .Register (& filePoolAt {})
212
- }
0 commit comments