001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.http.util;
027:
028: import java.nio.ByteBuffer;
029:
030: /**
031: *
032: *
033: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
034: * @version $Id: NioUtil.java,v 1.4 2004/02/26 16:51:54 lsimons Exp $
035: */
036: public class NioUtil {
037: /**
038: * Append the bytes in a buffer, converted to characters
039: * (where 1 byte -> 1 char, not 2 bytes -> 1 char), to
040: * the provided StringBuffer.
041: *
042: * @param buffer
043: * @param byteBuffer
044: */
045: public static void append(final StringBuffer buffer,
046: final ByteBuffer byteBuffer) {
047: final byte[] bytes = new byte[byteBuffer.remaining()];
048: byteBuffer.get(bytes);
049: final char[] chars = new char[bytes.length];
050: for (int i = 0; i < bytes.length; i++) {
051: chars[i] = (char) bytes[i];
052: }
053: byteBuffer.rewind();
054: buffer.append(chars);
055: }
056:
057: /**
058: * Convert the bytes in a buffer to characters and return
059: * them (where 1 byte -> 1 char, not 2 bytes -> 1 char).
060: *
061: * @param byteBuffer
062: * @return
063: */
064: public static char[] toCharArray(final ByteBuffer byteBuffer) {
065: if (byteBuffer == null)
066: return null;
067:
068: final byte[] bytes = new byte[byteBuffer.remaining()];
069: byteBuffer.get(bytes);
070: final char[] chars = new char[bytes.length];
071: for (int i = 0; i < bytes.length; i++) {
072: chars[i] = (char) bytes[i];
073: }
074: byteBuffer.rewind();
075: return chars;
076: }
077:
078: /**
079: * Convert the bytes in a buffer to characters and return
080: * them (where 1 byte -> 1 char, not 2 bytes -> 1 char) as
081: * a String.
082: *
083: * @param byteBuffer
084: * @return
085: */
086: public static String toString(final ByteBuffer byteBuffer) {
087: if (byteBuffer == null)
088: return null;
089:
090: return new String(toCharArray(byteBuffer));
091: }
092:
093: /**
094: * Convert the bytes in a set of buffers to characters and return
095: * them (where 1 byte -> 1 char, not 2 bytes -> 1 char) as
096: * a String.
097: *
098: * @param byteBuffers
099: * @return
100: */
101: public static String toString(final ByteBuffer[] byteBuffers) {
102: if (byteBuffers == null || byteBuffers.length == 0)
103: return null;
104:
105: final StringBuffer buffer = new StringBuffer();
106: for (int i = 0; i < byteBuffers.length; i++) {
107: final ByteBuffer byteBuffer = byteBuffers[i];
108: buffer.append(toCharArray(byteBuffer));
109: }
110: return buffer.toString();
111: }
112:
113: public static ByteBuffer toByteBuffer(final String string) {
114: if (string == null)
115: return null;
116:
117: final ByteBuffer bbuf = ByteBuffer.allocate(string.length());
118: final StringBuffer sbuf = new StringBuffer(string);
119: for (int i = 0; i < string.length(); i++) {
120: bbuf.put((byte) sbuf.charAt(i));
121: }
122: bbuf.rewind();
123: return bbuf;
124: }
125:
126: public static ByteBuffer toByteBuffer(final int num) {
127: final String string = "" + num;
128: return toByteBuffer(string);
129: }
130:
131: public static int toInteger(final ByteBuffer byteBuffer) {
132: if (byteBuffer == null)
133: return 0;
134:
135: return new Integer(new String(toCharArray(byteBuffer)))
136: .intValue();
137: }
138: }
|