1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.foundation;
20
21 /***
22 * A pure java implementation of NSMutableData, which
23 * is basically an editable wrapper for a byte array.
24 *
25 * @author michael@mpowers.net
26 * @author $Author: cgruber $
27 * @version $Revision: 892 $
28 */
29 public class NSMutableData
30 extends NSData
31 {
32 /***
33 * Default constructor creates a zero-data object.
34 */
35 public NSMutableData ()
36 {
37 super();
38 }
39
40 /***
41 * Creates an object containing the contents of the specified URL.
42 */
43 public NSMutableData (java.net.URL aURL)
44 {
45 super( aURL );
46 }
47
48 /***
49 * Creates an object containing a copy of the contents of the
50 * specified NSData object.
51 */
52 public NSMutableData (NSData aData)
53 {
54 super( aData );
55 }
56
57 /***
58 * Creates an object containing the specified number of bytes
59 * initialized to all zeroes.
60 */
61 public NSMutableData (int size)
62 {
63 super( new byte[size] );
64 }
65
66
67 /***
68 * Sets the length of the data to the specified length.
69 * If shorter, the data is truncated. If longer, the extra
70 * bytes are initialized to zeroes.
71 */
72 public void setLength (int length)
73 {
74 byte[] data = new byte[ length ];
75 int limit = length;
76 if (limit > bytes.length)
77 limit = bytes.length;
78 for ( int i = 0; i < limit; i++ )
79 {
80 data[i] = this.bytes[ i ];
81 }
82 this.bytes = data;
83 }
84
85 /***
86 * Appends the specified data to the end of this data.
87 */
88 public void appendData (NSData aData)
89 {
90 int len = aData.length();
91 byte[] data = new byte[ bytes.length + len ];
92
93 int i;
94 for ( i = 0; i < bytes.length; i++ )
95 {
96 data[i] = bytes[i];
97 }
98
99 byte[] src = aData.bytes( 0, len );
100 for ( int j = 0; j < len; j++ )
101 {
102 data[i+j] = src[j];
103 }
104
105 bytes = data;
106 }
107
108 public void appendByte(byte b) {
109 setLength(bytes.length + 1);
110 bytes[bytes.length-1] = b;
111 }
112
113 public void appendBytes(byte[] b) {
114 int origLen = bytes.length;
115 setLength(origLen + b.length);
116 for (int i = 0; i < b.length; i++)
117 bytes[i + origLen] = b[i];
118 }
119
120 /***
121 * Increases the size of the byte array by the specified amount.
122 */
123 public void increaseLengthBy (int increment)
124 {
125 setLength( length() + increment );
126 }
127
128 /***
129 * Sets the bytes in the array within the specified range to zero.
130 */
131 public void resetBytesInRange (NSRange aRange)
132 {
133 int loc = aRange.location();
134 int max = aRange.maxRange();
135 for ( int i = loc; i < max; i++ )
136 {
137 bytes[i] = 0;
138 }
139 }
140
141 /***
142 * Copies the data in the specified object to this object,
143 * completely replacing the previous contents.
144 */
145 public void setData (NSData aData)
146 {
147 bytes = aData.bytes( 0, aData.length() );
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167