summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableData.java
blob: c677ea7f95a69f55d45088c28fbca859a3320b58 (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
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.foundation;

/**
* A pure java implementation of NSMutableData, which
* is basically an editable wrapper for a byte array.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 892 $
*/
public class NSMutableData
    extends NSData
{
    /**
    * Default constructor creates a zero-data object.
    */
    public NSMutableData ()
    {
    	super();
    }

    /**
    * Creates an object containing the contents of the specified URL.
    */
    public NSMutableData (java.net.URL aURL)
    {
    	super( aURL );
    }

    /**
    * Creates an object containing a copy of the contents of the 
    * specified NSData object.
    */
    public NSMutableData (NSData aData)
    {
    	super( aData );
    }

    /**
    * Creates an object containing the specified number of bytes
    * initialized to all zeroes.
    */
    public NSMutableData (int size)
    {
		super( new byte[size] ); // inits to zeroes
    }


	/**
	* Sets the length of the data to the specified length.
	* If shorter, the data is truncated.  If longer, the extra
	* bytes are initialized to zeroes.
	*/
    public void setLength (int length)
    {
    	byte[] data = new byte[ length ]; // inits to zeroes
    	int limit = length;
    	if (limit > bytes.length)
    		limit = bytes.length;
	    for ( int i = 0; i < limit; i++ )
	    {
	    	data[i] = this.bytes[ i ];
	    }
	    this.bytes = data;
    }

	/**
	* Appends the specified data to the end of this data.
	*/
    public void appendData (NSData aData)
    {
    	int len = aData.length(); 
    	byte[] data = new byte[ bytes.length + len ];

	    int i;
	    for ( i = 0; i < bytes.length; i++ )
	    {
	    	data[i] = bytes[i];
	    }

	    byte[] src = aData.bytes( 0, len );
	    for ( int j = 0; j < len; j++ )
	    {
	    	data[i+j] = src[j];
	    }

	    bytes = data;
    }

	public void appendByte(byte b) {
		setLength(bytes.length + 1);
		bytes[bytes.length-1] = b;
	}

	public void appendBytes(byte[] b) {
		int origLen = bytes.length;
		setLength(origLen + b.length);
		for (int i = 0; i < b.length; i++)
			bytes[i + origLen] = b[i];
	}

	/**
	* Increases the size of the byte array by the specified amount.
	*/
    public void increaseLengthBy (int increment)
    {
    	setLength( length() + increment );
    }

	/**
	* Sets the bytes in the array within the specified range to zero.
	*/
    public void resetBytesInRange (NSRange aRange)
    {
    	int loc = aRange.location();
	    int max = aRange.maxRange();
	    for ( int i = loc; i < max; i++ )
	    {
	    	bytes[i] = 0;
	    }
    }

	/**
	* Copies the data in the specified object to this object,
	* completely replacing the previous contents.
	*/
    public void setData (NSData aData)
    {
		bytes = aData.bytes( 0, aData.length() );
    }
}

/*
 * $Log$
 * Revision 1.1  2006/02/16 12:47:16  cgruber
 * Check in all sources in eclipse-friendly maven-enabled packages.
 *
 * Revision 1.2  2003/08/06 23:57:13  chochos
 * appendByte(), appendBytes()
 *
 * Revision 1.1.1.1  2000/12/21 15:47:34  mpowers
 * Contributing wotonomy.
 *
 * Revision 1.3  2000/12/20 16:25:38  michael
 * Added log to all files.
 *
 *
 */