View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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] ); // inits to zeroes
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 ]; // inits to zeroes
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  * $Log$
153  * Revision 1.1  2006/02/16 12:47:16  cgruber
154  * Check in all sources in eclipse-friendly maven-enabled packages.
155  *
156  * Revision 1.2  2003/08/06 23:57:13  chochos
157  * appendByte(), appendBytes()
158  *
159  * Revision 1.1.1.1  2000/12/21 15:47:34  mpowers
160  * Contributing wotonomy.
161  *
162  * Revision 1.3  2000/12/20 16:25:38  michael
163  * Added log to all files.
164  *
165  *
166  */
167