]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/save/xml/ByteBuffer.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / save / xml / ByteBuffer.java
1 package tim.prune.save.xml;
2
3 import java.nio.charset.Charset;
4
5 /**
6  * Class to collect the bytes from an input stream
7  * and turn them into a String
8  */
9 public class ByteBuffer
10 {
11         // Array of bytes
12         private byte[] _bytes = new byte[1024];
13         // Current position to append
14         private int _currPos = 0;
15         // Flag for recognising utf8 encoded streams
16         private boolean _streamUtf8 = false;
17         // Flag for whether system is utf8 or not
18         private final boolean _systemUtf8 = XmlUtils.isSystemUtf8();
19
20         /**
21          * Append the given byte to the buffer
22          * @param inB byte to append
23          */
24         public void appendByte(byte inB)
25         {
26                 // Resize array if necessary
27                 if (_currPos >= _bytes.length)
28                 {
29                         byte[] bigger = new byte[_bytes.length * 2];
30                         System.arraycopy(_bytes, 0, bigger, 0, _bytes.length);
31                         _bytes = bigger;
32                 }
33                 // Append byte and increment counter
34                 _bytes[_currPos] = inB;
35                 _currPos++;
36         }
37
38         /**
39          * Clear the buffer and reset
40          */
41         public void clear()
42         {
43                 _currPos = 0;
44                 // Reduce size back to default if it's got too big
45                 if (_bytes.length > 5000) {
46                         _bytes = new byte[1024];
47                 }
48         }
49
50         /**
51          * Set the flag that this stream is encoded with utf8
52          */
53         public void setEncodingUtf8() {
54                 _streamUtf8 = true;
55         }
56
57         /**
58          * @return contents of buffer as a String
59          */
60         public String toString()
61         {
62                 // Sometimes the encoding of the read file isn't the default encoding of the system
63                 if (_streamUtf8 && !_systemUtf8)
64                 {
65                         return new String(_bytes, 0, _currPos, Charset.forName("UTF-8"));
66                 }
67                 // Otherwise just use system encoding
68                 return new String(_bytes, 0, _currPos);
69         }
70
71         /**
72          * Look for the given character sequence in the last characters read
73          * @param inChars sequence to look for
74          * @return true if sequence found
75          */
76         public boolean foundSequence(char[] inChars)
77         {
78                 final int numChars = inChars.length;
79                 if (_currPos < numChars) {return false;}
80                 for (int i=0; i<numChars; i++)
81                 {
82                         char searchChar = inChars[numChars - 1 - i];
83                         char sourceChar = (char) _bytes[_currPos - 1 - i];
84                         if (searchChar != sourceChar) {return false;}
85                 }
86                 return true;
87         }
88 }