001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.jsr82emul;
027:
028: /**
029: * Utility class that packs various data to resores from byte array.
030: * An instance refers to byte array that keeps packed data and passed
031: * to the instance thru either constructor or reset() method.
032: */
033: public class BytePack {
034: /** Current offset in byte array */
035: protected int offset;
036: /** Byte array that keeps packed data */
037: protected byte[] buffer;
038:
039: /**
040: * Constructs an instance with given byte array.
041: * @param buffer byte array to either unpak data from or pack into.
042: */
043: public BytePack(byte[] buffer) {
044: reset(buffer);
045: }
046:
047: /** Constructs an instance. */
048: public BytePack() {
049: }
050:
051: /**
052: * Resets state, setting packed data destination to given array
053: * and current offset to 0.
054: *
055: * @param buffer new byte array to either unpak data from or pack into.
056: */
057: public void reset(byte[] buffer) {
058: offset = 0;
059: this .buffer = buffer;
060: }
061:
062: /** Resets state, setting current offset to 0. */
063: public void reset() {
064: offset = 0;
065: }
066:
067: /**
068: * Removes internal reference to current byte array, returning
069: * the reference outside.
070: * @return (reference to) byte array processed.
071: */
072: public byte[] release() {
073: byte[] tmp = buffer;
074: buffer = null;
075: return tmp;
076: }
077:
078: /**
079: * Appends byte to packed data.
080: * @param b byte to append.
081: */
082: public void append(byte b) {
083: buffer[offset++] = b;
084: }
085:
086: /**
087: * Appends given bytes to packed data.
088: * @param bytes bytes to append.
089: */
090: public void appendBytes(byte[] bytes) {
091: System.arraycopy(bytes, 0, buffer, offset, bytes.length);
092: offset += bytes.length;
093: }
094:
095: /**
096: * Appends a 4-byte rpresentation of given integer value to packed data.
097: * The value can be restored by extractInt() method.
098: * @param value value to append
099: */
100: public void appendInt(int value) {
101: for (int i = 0; i < 4; i++) {
102: buffer[offset++] = (byte) value;
103: value >>= 8;
104: }
105: }
106:
107: /**
108: * Extracts integer value previously stored by appendInt() from byte
109: * represnetation.
110: * @return value extracted
111: */
112: public int extractInt() {
113: int value = 0;
114: for (int i = 0; i < 4; i++) {
115: value |= (buffer[offset++] & 0xff) << (8 * i);
116: }
117: return value;
118: }
119:
120: /**
121: * Extracts next byte from packed data.
122: * @return byte extracted.
123: */
124: public byte extract() {
125: return buffer[offset++];
126: }
127:
128: /**
129: * Extracts next requested amount of bytes from packed data.
130: * @param length amount of bytes to extract
131: * @return (refernce to) newly created byte array that contains bytes
132: * extracted.
133: */
134: public byte[] extractBytes(int length) {
135: byte[] bytes = new byte[length];
136: System.arraycopy(buffer, offset, bytes, 0, length);
137: offset += length;
138: return bytes;
139: }
140: }
|