KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > DRDAString


1 /*
2  * Derby - class org.apache.derby.impl.drda.DRDAString
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements. See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16  * implied. See the License for the specific language governing
17  * permissions and limitations under the License.
18  *
19  */

20
21 package org.apache.derby.impl.drda;
22
23 /**
24  * This class provides functionality for reusing buffers and strings
25  * when parsing DRDA packets. A byte array representing a string is
26  * stored internally. When the string is requested as a
27  * <code>String</code> object, the byte array is converted to a
28  * string, and the string is cached to avoid unnecessary conversion
29  * later.
30  */

31 final class DRDAString {
32     /** Buffer representing the string. */
33     private byte[] buffer;
34     /** Object used to convert byte buffer to string. */
35     private final CcsidManager ccsidManager;
36
37     /** True if the contents were modified in the previous call to
38      * <code>setBytes</code>. */

39     private boolean modified;
40
41     /** The previously generated string. */
42     private String JavaDoc cachedString;
43
44     /**
45      * Create a new <code>DRDAString</code> instance.
46      *
47      * @param m a <code>CcsidManager</code> value specifying
48      * which encoding is used
49      */

50     DRDAString(CcsidManager m) {
51         this.buffer = new byte[0];
52         this.ccsidManager = m;
53         this.cachedString = null;
54     }
55
56     /**
57      * Check whether the internal buffer contains the same data as
58      * another byte buffer.
59      *
60      * @param buf a byte array
61      * @param offset start position in the byte array
62      * @param size how many bytes to read from the byte array
63      * @return <code>true</code> if the internal buffer contains the
64      * same data as the specified byte array
65      */

66     private boolean equalTo(byte[] buf, int offset, int size) {
67         int len = buffer.length;
68         if (len != size) return false;
69         for (int i = 0; i < len; ++i) {
70             if (buffer[i] != buf[i+offset]) return false;
71         }
72         return true;
73     }
74
75     /**
76      * Modify the internal byte buffer. If the new data is equal to
77      * the old data, the cached values are not cleared.
78      *
79      * @param src the new bytes
80      * @param offset start offset
81      * @param size number of bytes to use
82      */

83     public void setBytes(byte[] src, int offset, int size) {
84         if (equalTo(src, offset, size)) {
85             modified = false;
86             return;
87         }
88         if (buffer.length != size) {
89             buffer = new byte[size];
90         }
91         System.arraycopy(src, offset, buffer, 0, size);
92         modified = true;
93         cachedString = null;
94     }
95
96     /**
97      * Check whether the contents of the <code>DRDAString</code> were
98      * modified in the previous call to <code>setBytes()</code>.
99      *
100      * @return <code>true</code> if the contents were modified
101      */

102     public boolean wasModified() {
103         return modified;
104     }
105
106     /**
107      * Convert the internal byte array to a string. The string value
108      * is cached.
109      *
110      * @return a <code>String</code> value
111      */

112     public String JavaDoc toString() {
113         if (cachedString == null) {
114             cachedString =
115                 ccsidManager.convertToUCS2(buffer);
116         }
117         return cachedString;
118     }
119
120     /**
121      * Return the length in bytes of the internal string
122      * representation.
123      *
124      * @return length of internal representation
125      */

126     public int length() {
127         return buffer.length;
128     }
129
130     /**
131      * Return the internal byte array. The returned array should not
132      * be modified, as it is used internally in
133      * <code>DRDAString</code>. The value of the array might be
134      * modified by subsequent calls to
135      * <code>DRDAString.setBytes()</code>.
136      *
137      * @return internal buffer
138      */

139     public byte[] getBytes() {
140         return buffer;
141     }
142 }
143
Popular Tags