KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ice > jni > registry > RegMultiStringValue


1 /*
2 ** Java native interface to the Windows Registry API.
3 ** Copyright (c) 1997 by Timothy Gerard Endres
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package com.ice.jni.registry;
24
25 import java.io.PrintWriter JavaDoc;
26
27
28 /**
29  * The RegMultiStringValue class represents a multiple
30  * string, or string array, value in the registry
31  * (REG_MULTI_SZ).
32  *
33  * @see com.ice.jni.registry.Registry
34  * @see com.ice.jni.registry.RegistryKey
35  */

36
37 public class
38 RegMultiStringValue extends RegistryValue
39     {
40     String JavaDoc[] data;
41     int dataLen;
42
43
44     public
45     RegMultiStringValue( RegistryKey key, String JavaDoc name )
46         {
47         super( key, name, RegistryValue.REG_MULTI_SZ );
48         this.data = null;
49         this.dataLen = 0;
50         }
51
52     public
53     RegMultiStringValue( RegistryKey key, String JavaDoc name, int type )
54         {
55         super( key, name, type );
56         this.data = null;
57         this.dataLen = 0;
58         }
59
60     public
61     RegMultiStringValue( RegistryKey key, String JavaDoc name, String JavaDoc[] data )
62         {
63         super( key, name, RegistryValue.REG_MULTI_SZ );
64         this.setData( data );
65         }
66
67     public String JavaDoc[]
68     getData()
69         {
70         return this.data;
71         }
72
73     public int
74     getLength()
75         {
76         return this.dataLen;
77         }
78
79     public void
80     setData( String JavaDoc[] data )
81         {
82         this.data = data;
83         this.dataLen = data.length;
84         }
85
86     public byte[]
87     getByteData()
88         {
89         int len = this.getByteLength();
90
91         int ri = 0;
92         byte[] result = new byte[len];
93         for ( int i = 0 ; i < this.dataLen ; ++i )
94             {
95             byte[] strBytes = this.data[i].getBytes();
96
97             for ( int j = 0 ; j < strBytes.length ; ++j )
98                 result[ri++] = strBytes[j];
99
100             result[ri++] = 0;
101             }
102
103         return result;
104         }
105
106     public int
107     getByteLength()
108         {
109         int len = 0;
110         for ( int i = 0 ; i < this.dataLen ; ++i )
111             len += this.data[i].length() + 1;
112
113         return len;
114         }
115
116     public void
117     setByteData( byte[] data )
118         {
119         int start;
120         int count = 0;
121
122         for ( int i = 0 ; i < data.length ; ++i )
123             {
124             if ( data[i] == 0 )
125                 count++;
126             }
127
128         int si = 0;
129         String JavaDoc[] newData = new String JavaDoc[ count ];
130         for ( int i = start = 0 ; i < data.length ; ++i )
131             {
132             if ( data[i] == 0 )
133                 {
134                 newData[si] = new String JavaDoc( data, start, (i - start) );
135                 start = si;
136                 }
137             }
138
139         this.setData( newData );
140         }
141
142     public void
143     export( PrintWriter JavaDoc out )
144         {
145         byte[] hexData;
146         int dataLen = 0;
147
148         out.println( "\"" + this.getName() + "\"=hex(7):\\" );
149
150         for ( int i = 0 ; i < this.data.length ; ++i )
151             {
152             dataLen += this.data[i].length() + 1;
153             }
154
155         ++dataLen;
156
157         int idx = 0;
158         hexData = new byte[ dataLen ];
159
160         for ( int i = 0 ; i < this.data.length ; ++i )
161             {
162             int strLen = this.data[i].length();
163             byte[] strBytes = this.data[i].getBytes();
164
165             System.arraycopy
166                 ( strBytes, 0, hexData, idx, strLen );
167             
168             idx += strLen;
169
170             hexData[ idx++ ] = '\0';
171             }
172
173         hexData[ idx++ ] = '\0';
174
175         RegistryValue.exportHexData( out, hexData );
176         }
177
178     }
179
180
181
182
183
Popular Tags