Source Code Cross Referenced for ByteUtils.java in  » ERP-CRM-Financial » sakai » edu » indiana » lib » twinpeaks » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » sakai » edu.indiana.lib.twinpeaks.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**********************************************************************************
002:         *
003:         * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004:         *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005:         *
006:         * Licensed under the Educational Community License Version 1.0 (the "License");
007:         * By obtaining, using and/or copying this Original Work, you agree that you have read,
008:         * understand, and will comply with the terms and conditions of the Educational Community License.
009:         * You may obtain a copy of the License at:
010:         *
011:         *      http://cvs.sakaiproject.org/licenses/license_1_0.html
012:         *
013:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014:         * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015:         * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016:         * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017:         * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018:         *
019:         **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021:        import java.io.*;
022:        import java.util.*;
023:
024:        /**
025:         * Byte utilities
026:         */
027:        public class ByteUtils {
028:
029:            private static org.apache.commons.logging.Log _log = LogUtils
030:                    .getLog(ByteUtils.class);
031:
032:            /*
033:             * Byte manipulation utilities
034:             *
035:             * Private constructor
036:             */
037:            private ByteUtils() {
038:            }
039:
040:            /**
041:             * Returns the index in the source array where the first occurrence
042:             * of the specified text (a String, converted to a byte array) is found
043:             *
044:             * @param source Byte array to examine
045:             * @param matchString String to locate in <code>source</code>
046:             * @return Index of the first matching character (-1 if no match)
047:             */
048:            public static int indexOf(byte[] source, String matchString) {
049:                return indexOf(source, matchString.getBytes());
050:            }
051:
052:            /**
053:             * Returns the index in the source array where the first occurrence
054:             * of the specified byte pattern is found
055:             *
056:             * @param source Byte array to examine
057:             * @param match Byte array to locate in <code>source</code>
058:             * @return Index of the first matching character (-1 if no match)
059:             */
060:            public static int indexOf(byte[] source, byte[] match) {
061:                for (int i = 0; i < source.length; i++) {
062:                    if (startsWith(source, i, match)) {
063:                        return i;
064:                    }
065:                }
066:                return -1;
067:            }
068:
069:            /**
070:             * Returns the index in the source array where the last occurrence
071:             * of the specified text (a String, converted to a byte array) is found
072:             *
073:             * @param source Byte array to examine
074:             * @param matchString String to locate in <code>source</code>
075:             * @return Index of the first matching character (-1 if no match)
076:             */
077:            public static int lastIndexOf(byte[] source, String matchString) {
078:                return lastIndexOf(source, matchString.getBytes());
079:            }
080:
081:            /**
082:             * Returns the index in the source array where the last occurrence
083:             * of the specified byte pattern is found
084:             *
085:             * @param source Byte array to examine
086:             * @param match Byte array to locate in <code>source</code>
087:             * @return Index of the last matching character (-1 if no match)
088:             */
089:            public static int lastIndexOf(byte[] source, byte[] match) {
090:
091:                if (source.length < match.length) {
092:                    return -1;
093:                }
094:
095:                for (int i = (source.length - match.length); i >= 0; i--) {
096:                    if (startsWith(source, i, match)) {
097:                        return i;
098:                    }
099:                }
100:                return -1;
101:            }
102:
103:            /**
104:             * Does this byte array begin with match array content?
105:             *
106:             * @param source Byte array to examine
107:             * @param match Byte array to locate in <code>source</code>
108:             * @return true If the starting bytes are equal
109:             */
110:            public static boolean startsWith(byte[] source, byte[] match) {
111:                return startsWith(source, 0, match);
112:            }
113:
114:            /**
115:             * Does this byte array begin with match array content?
116:             *
117:             * @param source Byte array to examine
118:             * @param offset An offset into the <code>source</code> array
119:             * @param match Byte array to locate in <code>source</code>
120:             * @return true If the starting bytes are equal
121:             */
122:            public static boolean startsWith(byte[] source, int offset,
123:                    byte[] match) {
124:
125:                if (match.length > (source.length - offset)) {
126:                    return false;
127:                }
128:
129:                for (int i = 0; i < match.length; i++) {
130:                    if (source[offset + i] != match[i]) {
131:                        return false;
132:                    }
133:                }
134:                return true;
135:            }
136:
137:            /**
138:             * Does the source array equal the match array?
139:             *
140:             * @param source Byte array to examine
141:             * @param offset An offset into the <code>source</code> array
142:             * @param match Byte array to locate in <code>source</code>
143:             * @return true If the two arrays are equal
144:             */
145:            public static boolean equals(byte[] source, byte[] match) {
146:
147:                if (match.length != source.length) {
148:                    return false;
149:                }
150:                return startsWith(source, 0, match);
151:            }
152:
153:            /**
154:             * Copies bytes from the source byte array to the destination array
155:             *
156:             * @param source The source array
157:             * @param srcBegin Index of the first source byte to copy
158:             * @param srcEnd Index after the last source byte to copy
159:             * @param destination The destination array
160:             * @param dstBegin The starting offset in the destination array
161:             */
162:            public static void getBytes(byte[] source, int srcBegin,
163:                    int srcEnd, byte[] destination, int dstBegin) {
164:                System.arraycopy(source, srcBegin, destination, dstBegin,
165:                        srcEnd - srcBegin);
166:            }
167:
168:            /**
169:             * Return a new byte array containing a sub-portion of the source array
170:             *
171:             * @param srcBegin The beginning index (inclusive)
172:             * @param srcEnd  The ending index (exclusive)
173:             * @return The new, populated byte array
174:             */
175:            public static byte[] subbytes(byte[] source, int srcBegin,
176:                    int srcEnd) {
177:                byte destination[];
178:
179:                destination = new byte[srcEnd - srcBegin];
180:                getBytes(source, srcBegin, srcEnd, destination, 0);
181:
182:                return destination;
183:            }
184:
185:            /**
186:             * Return a new byte array containing a sub-portion of the source array
187:             *
188:             * @param srcBegin The beginning index (inclusive)
189:             * @return The new, populated byte array
190:             */
191:            public static byte[] subbytes(byte[] source, int srcBegin) {
192:                return subbytes(source, srcBegin, source.length);
193:            }
194:
195:            /*
196:             * Test
197:             */
198:            public static void main(String[] args) throws Exception {
199:                byte byteText[];
200:                int index, last;
201:
202:                byteText = args[0].getBytes();
203:                index = indexOf(byteText, "XXX");
204:                last = lastIndexOf(byteText, "XXX");
205:
206:                _log.debug("Index = " + index);
207:                _log.debug("Last  = " + last);
208:                _log.debug("equal = " + equals(byteText, "XXX".getBytes()));
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.