summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/main/java/org/simpleframework/common/buffer/FileBuffer.java
blob: 8fcea77e4c94e65938c1b758846673e8bed9aee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * FileBuffer.java February 2008
 *
 * Copyright (C) 2008, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.common.buffer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * The <code>FileBuffer</code> object is used to create a buffer
 * which will write the appended data to an underlying file. This
 * is typically used for buffers that are too large for to allocate
 * in memory. Data appended to the buffer can be retrieved at a 
 * later stage by acquiring the <code>InputStream</code> for the
 * underlying file. To ensure that excessive file system space is
 * not occupied the buffer files are cleaned every five minutes.
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.common.buffer.FileAllocator
 */
class FileBuffer implements Buffer {
   
   /**
    * This is the file output stream used for this buffer object.
    */
   private OutputStream buffer;
   
   /**
    * This represents the last file segment that has been created.
    */
   private Segment segment;
   
   /**
    * This is the path for the file that this buffer appends to.
    */
   private File file;
   
   /**
    * This is the number of bytes currently appended to the buffer.
    */
   private long count;
   
   /**
    * This is used to determine if this buffer has been closed.
    */
   private boolean closed;
   
   /**
    * Constructor for the <code>FileBuffer</code> object. This will
    * create a buffer using the provided file. All data appended to
    * this buffer will effectively written to the underlying file. 
    * If the appended data needs to be retrieved at a later stage
    * then it can be acquired using the buffers input stream.
    * 
    * @param file this is the file used for the file buffer
    */
   public FileBuffer(File file) throws IOException {
      this.buffer  = new FileOutputStream(file);
      this.file = file;
   }

   /**
    * This is used to allocate a segment within this buffer. If the
    * buffer is closed this will throw an exception, if however the
    * buffer is still open then a segment is created which will 
    * write all appended data to this buffer. However it can be
    * treated as an independent source of data.
    * 
    * @return this returns a buffer which is a segment of this 
    */
   public Buffer allocate() throws IOException {
      if(closed) {
         throw new BufferException("Buffer has been closed");
      }
      if(segment != null) {
         segment.close();
      }
      if(!closed) {
         segment = new Segment(this, count);
      }
      return segment;
   }

   /**
    * This is used to append the specified data to the underlying 
    * file. All bytes appended to the file can be consumed at a
    * later stage by acquiring the <code>InputStream</code> from
    * this buffer. Also if require the data can be encoded as a
    * string object in a required character set.
    * 
    * @param array this is the array to write the the file
    * 
    * @return this returns this buffer for further operations
    */
   public Buffer append(byte[] array) throws IOException {
      return append(array, 0, array.length);
   }

   /**
    * This is used to append the specified data to the underlying 
    * file. All bytes appended to the file can be consumed at a
    * later stage by acquiring the <code>InputStream</code> from
    * this buffer. Also if require the data can be encoded as a
    * string object in a required character set.
    * 
    * @param array this is the array to write the the file
    * @param off this is the offset within the array to write
    * @param size this is the number of bytes to be appended
    * 
    * @return this returns this buffer for further operations
    */
   public Buffer append(byte[] array, int off, int size) throws IOException {
      if(closed) {
         throw new BufferException("Buffer has been closed");
      }
      if(size > 0) {
         buffer.write(array, off, size);
         count += size;
      }
      return this;
   }

   /**
    * This method is used to acquire the buffered bytes as a string.
    * This is useful if the contents need to be manipulated as a
    * string or transferred into another encoding. If the UTF-8
    * content encoding is not supported the platform default is 
    * used, however this is unlikely as UTF-8 should be supported.
    *
    * @return this returns a UTF-8 encoding of the buffer contents
    */ 
   public String encode() throws IOException {
      return encode("UTF-8");
   }

   /**
    * This method is used to acquire the buffered bytes as a string.
    * This is useful if the contents need to be manipulated as a
    * string or transferred into another encoding. This will convert
    * the bytes using the specified character encoding format.
    * 
    * @param charset this is the charset to encode the data with
    *
    * @return this returns the encoding of the buffer contents
    */   
   public String encode(String charset) throws IOException {
      InputStream source = open();
      int size = (int)count;
      
      if(count <= 0) {
         return new String();
      }
      return convert(source, charset, size); 
   }
   
   /**
    * This method is used to acquire the buffered bytes as a string.
    * This is useful if the contents need to be manipulated as a
    * string or transferred into another encoding. This will convert
    * the bytes using the specified character encoding format.
    * 
    * @param source this is the source stream that is to be encoded
    * @param charset this is the charset to encode the data with
    * @param count this is the number of bytes to be encoded
    *
    * @return this returns the encoding of the buffer contents
    */   
   private String convert(InputStream source, String charset, int count) throws IOException {
      byte[] buffer = new byte[count];
      int left = count;
      
      while(left > 0) {
         int size = source.read(buffer, 0, left);
         
         if(size == -1) {
            throw new BufferException("Could not read buffer");
         }
         left -= count;
      }
      return new String(buffer, charset);
   }

   /**
    * This method is used so that a buffer can be represented as a
    * stream of bytes. This provides a quick means to access the data
    * that has been written to the buffer. It wraps the buffer within
    * an input stream so that it can be read directly.
    *
    * @return a stream that can be used to read the buffered bytes
    */ 
   public InputStream open() throws IOException {
      if(!closed) {
         close();
      }
      return open(file);
   }
   
   /**
    * This method is used so that a buffer can be represented as a
    * stream of bytes. This provides a quick means to access the data
    * that has been written to the buffer. It wraps the buffer within
    * an input stream so that it can be read directly.
    *
    * @param file this is the file used to create the input stream
    *
    * @return a stream that can be used to read the buffered bytes
    */ 
   private InputStream open(File file) throws IOException {
      InputStream source = new FileInputStream(file);
      
      if(count <= 0) {
         source.close(); // release file descriptor
      }
      return new Range(source, count);
   }

   /**
    * This will clear all data from the buffer. This simply sets the
    * count to be zero, it will not clear the memory occupied by the
    * instance as the internal buffer will remain. This allows the
    * memory occupied to be reused as many times as is required.
    */   
   public void clear() throws IOException {
      if(closed) {
         throw new BufferException("Buffer has been closed");
      }
   }

   /**
    * This method is used to ensure the buffer can be closed. Once
    * the buffer is closed it is an immutable collection of bytes and
    * can not longer be modified. This ensures that it can be passed
    * by value without the risk of modification of the bytes.
    */   
   public void close() throws IOException {
      if(!closed) {
         buffer.close();
         closed = true;
      }
      if(segment != null) {
         segment.close();
      }
   }
   
   /**
    * This is used to provide the number of bytes that have been
    * written to the buffer. This increases as bytes are appended
    * to the buffer. if the buffer is cleared this resets to zero.
    *  
    * @return this returns the number of bytes within the buffer
    */
   public long length() {
      return count;
   }

   /**
    * The <code>Segment</code> object is used to create a segment of
    * the parent buffer. The segment will write to the parent however
    * if can be read as a unique range of bytes starting with the 
    * first sequence of bytes appended to the segment. A segment can
    * be used to create a collection of buffers backed by the same
    * underlying file, as is require with multipart uploads.
    */
   private class Segment implements Buffer {
        
      /**
       * This is an internal segment created from this buffer object.
       */
      private Segment segment;
      
      /**
       * This is the parent buffer that bytes are to be appended to.
       */
      private Buffer parent;
      
      /**
       * This is the offset of the first byte within the sequence.
       */
      private long first;
      
      /**
       * This is the last byte within the segment for this segment.
       */
      private long last;
      
      /**
       * This determines if the segment is currently open or closed.
       */
      private boolean closed;
      
      /**
       * Constructor for the <code>Segment</code> object. This is used
       * to create a segment from a parent buffer. A segment is a part
       * of the parent buffer and appends its bytes to the parent. It
       * can however be treated as an independent source of bytes.
       * 
       * @param parent this is the parent buffer to be appended to
       * @param first this is the offset for the first byte in this
       */
      public Segment(Buffer parent, long first) {
         this.parent = parent;
         this.first = first;
         this.last = first;
      }

      /**
       * This is used to allocate a segment within this buffer. If the
       * buffer is closed this will throw an exception, if however the
       * buffer is still open then a segment is created which will 
       * write all appended data to this buffer. However it can be
       * treated as an independent source of data.
       * 
       * @return this returns a buffer which is a segment of this 
       */
      public Buffer allocate() throws IOException {
         if(closed) {
            throw new BufferException("Buffer has been closed");
         }
         if(segment != null) {
            segment.close();
         }
         if(!closed) {
            segment = new Segment(this, last);
         }
         return segment;
      }

      /**
       * This is used to append the specified data to the underlying 
       * file. All bytes appended to the file can be consumed at a
       * later stage by acquiring the <code>InputStream</code> from
       * this buffer. Also if require the data can be encoded as a
       * string object in a required character set.
       * 
       * @param array this is the array to write the the file
       * 
       * @return this returns this buffer for further operations
       */
      public Buffer append(byte[] array) throws IOException {
         return append(array, 0, array.length);
      }

      /**
       * This is used to append the specified data to the underlying 
       * file. All bytes appended to the file can be consumed at a
       * later stage by acquiring the <code>InputStream</code> from
       * this buffer. Also if require the data can be encoded as a
       * string object in a required character set.
       * 
       * @param array this is the array to write the the file
       * @param off this is the offset within the array to write
       * @param size this is the number of bytes to be appended
       * 
       * @return this returns this buffer for further operations
       */
      public Buffer append(byte[] array, int off, int size) throws IOException {
         if(closed) {
            throw new BufferException("Buffer has been closed");
         }
         if(size > 0) {
            parent.append(array, off, size);
            last += size;
         }
         return this;
      }

      /**
       * This method is used to acquire the buffered bytes as a string.
       * This is useful if the contents need to be manipulated as a
       * string or transferred into another encoding. If the UTF-8
       * content encoding is not supported the platform default is 
       * used, however this is unlikely as UTF-8 should be supported.
       *
       * @return this returns a UTF-8 encoding of the buffer contents
       */ 
      public String encode() throws IOException {
         return encode("UTF-8");
      }

      /**
       * This method is used to acquire the buffered bytes as a string.
       * This is useful if the contents need to be manipulated as a
       * string or transferred into another encoding. This will convert
       * the bytes using the specified character encoding format.
       * 
       * @param charset this is the charset to encode the data with
       *
       * @return this returns the encoding of the buffer contents
       */  
      public String encode(String charset) throws IOException {
         InputStream source = open();
         long count = last - first;
         int size = (int)count;
         
         if(count <= 0) {
            return new String();
         }
         return convert(source, charset, size);    
      }

      /**
       * This method is used so that a buffer can be represented as a
       * stream of bytes. This provides a quick means to access the data
       * that has been written to the buffer. It wraps the buffer within
       * an input stream so that it can be read directly.
       *
       * @return a stream that can be used to read the buffered bytes
       */ 
      public InputStream open() throws IOException {
         InputStream source = new FileInputStream(file);
         long length = last - first;
         
         if(first > 0) {
            source.skip(first);
         }
         return new Range(source, length);
      }

      /**
       * This will clear all data from the buffer. This simply sets the
       * count to be zero, it will not clear the memory occupied by the
       * instance as the internal buffer will remain. This allows the
       * memory occupied to be reused as many times as is required.
       */  
      public void clear() throws IOException {
         if(closed) {
            throw new BufferException("Buffer is closed");
         }
      }

      /**
       * This method is used to ensure the buffer can be closed. Once
       * the buffer is closed it is an immutable collection of bytes and
       * can not longer be modified. This ensures that it can be passed
       * by value without the risk of modification of the bytes.
       */   
      public void close() throws IOException {
         if(!closed) {
            closed = true;
         }
         if(segment != null) {
            segment.close();
         }
      }

      /**
       * This determines how much space is left in the buffer. If there
       * is no limit to the buffer size this will return the maximum
       * long value. Typically this is the capacity minus the length.
       *
       * @return this is the space that is available within the buffer
       */
      public long space() {
         return Long.MAX_VALUE;
      }
      
      /**
       * This is used to provide the number of bytes that have been
       * written to the buffer. This increases as bytes are appended
       * to the buffer. if the buffer is cleared this resets to zero.
       *  
       * @return this returns the number of bytes within the buffer
       */
      public long length() {
         return last - first;
      }
      
   }
   
   /**
    * The <code>Range</code> object is used to provide a stream that
    * can read a range of bytes from a provided input stream. This
    * allows buffer segments to be allocated from the main buffer.
    * Providing a range in this manner ensures that only one backing
    * file is needed for the primary buffer allocated. 
    */
   private class Range extends FilterInputStream {
        
      /**
       * This is the length of the bytes that exist in the range.
       */
      private long length;
      
      /**
       * This is used to close the stream once it has been read.
       */
      private boolean closed;
      
      /**
       * Constructor for the <code>Range</code> object. This ensures
       * that only a limited number of bytes can be consumed from a
       * backing input stream giving the impression of an independent
       * stream of bytes for a segmented region of the parent buffer.
       * 
       * @param source this is the input stream used to read data
       * @param length this is the number of bytes that can be read
       */ 
      public Range(InputStream source, long length) {
         super(source);
         this.length = length;
      }
    
      /**
       * This will read data from the underlying stream up to the 
       * number of bytes this range is allowed to read. When all of
       * the bytes are exhausted within the stream this returns -1.
       * 
       * @return this returns the octet from the underlying stream
       */
      @Override
      public int read() throws IOException {
         if(length-- > 0) {
            return in.read();
         }
         if(length <= 0) {
            close();
         }
         return -1;
      }
      
      /**
       * This will read data from the underlying stream up to the 
       * number of bytes this range is allowed to read. When all of
       * the bytes are exhausted within the stream this returns -1.
       * 
       * @param array this is the array to read the bytes in to
       * @param off this is the start offset to append the bytes to
       * @param size this is the number of bytes that are required
       * 
       * @return this returns the number of bytes that were read
       */
      @Override
      public int read(byte[] array, int off, int size) throws IOException {
         int left = (int)Math.min(length, size);
         
         if(left > 0) {
            int count = in.read(array, off, left);
            
            if(count > 0){
               length -= count;
            }
            if(length <= 0) {
               close();
            }
            return count;
         }
         return -1;
      }
      
      /**
       * This returns the number of bytes that can be read from the
       * range. This will be the actual number of bytes the range
       * contains as the underlying file will not block reading.
       * 
       * @return this returns the number of bytes within the range
       */
      @Override
      public int available() throws IOException {
         return (int)length;
      }
      
      /**
       * This is the number of bytes to skip from the buffer. This 
       * will allow up to the number of remaining bytes within the
       * range to be read. When all the bytes have been read this
       * will return zero indicating no bytes were skipped.
       * 
       * @param size this returns the number of bytes to skip
       * 
       * @return this returns the number of bytes that were skipped
       */
      @Override
      public long skip(long size) throws IOException {
         long left = Math.min(length, size);
         long skip = in.skip(left);
         
         if(skip > 0) {
            length -= skip;
         } 
         if(length <= 0) {
            close();
         }
         return skip;
      }
      
      /**
       * This is used to close the range once all of the content has
       * been fully read. The <code>Range</code> object forces the
       * close of the stream once all the content has been consumed 
       * to ensure that excessive file descriptors are used. Also
       * this will ensure that the files can be deleted.
       */
      @Override
      public void close() throws IOException {
         if(!closed) {
            in.close();
            closed =true;
         }
      }
   }
}