001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor A. Pyankov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.x.print.ipp;
021:
022: import java.nio.ByteBuffer;
023:
024: public class IppByteBuffer {
025: byte[] buf;
026: ByteBuffer bbuf;
027: int count = 0;
028:
029: public IppByteBuffer() {
030: buf = new byte[512];
031: bbuf = ByteBuffer.wrap(buf);
032: }
033:
034: public IppByteBuffer(byte[] rb) {
035: buf = new byte[rb.length];
036: bbuf = ByteBuffer.wrap(buf);
037:
038: System.arraycopy(rb, 0, buf, 0, rb.length);
039: count = rb.length;
040: }
041:
042: protected int addcapacity(int add) {
043: byte[] newbuf = new byte[buf.length + ((add / 512) + 1) * 512];
044: ByteBuffer newbbuf = ByteBuffer.wrap(newbuf);
045:
046: System.arraycopy(buf, 0, newbuf, 0, buf.length);
047: buf = newbuf;
048: bbuf = newbbuf;
049:
050: return buf.length;
051: }
052:
053: public byte[] put(int index, byte value) {
054: if (index >= buf.length) {
055: addcapacity(index + 1 - buf.length);
056: }
057: //bbuf.put(index, value);
058: buf[index] = value;
059: count = (index + 1) > count ? index + 1 : count;
060:
061: return buf;
062: }
063:
064: public byte get(int index) {
065: return bbuf.get(index);
066: }
067:
068: public byte[] put(int index, short value) {
069: if ((index + 2) > buf.length) {
070: addcapacity(index + 2 - buf.length);
071: }
072: bbuf.putShort(index, value);
073: count = (index + 2) > count ? index + 2 : count;
074:
075: return buf;
076: }
077:
078: public short getShort(int index) {
079: return bbuf.getShort(index);
080: }
081:
082: public byte[] put(int index, int value) {
083: if ((index + 4) > buf.length) {
084: addcapacity(index + 4 - buf.length);
085: }
086: bbuf.putInt(index, value);
087: count = (index + 4) > count ? index + 4 : count;
088:
089: return buf;
090: }
091:
092: public int getInt(int index) {
093: return bbuf.getInt(index);
094: }
095:
096: public byte[] put(int index, byte[] value) {
097: if ((index + value.length) > buf.length) {
098: addcapacity(index + value.length - buf.length);
099: }
100: System.arraycopy(value, 0, buf, index, value.length);
101: count = (index + value.length) > count ? index + value.length
102: : count;
103:
104: return buf;
105: }
106:
107: public byte[] get(int index, int length) {
108: byte[] bb = new byte[length];
109:
110: System.arraycopy(buf, index, bb, 0, length);
111: return bb;
112: }
113:
114: public byte[] getBytes() {
115: return get(0, count);
116: }
117:
118: public byte[] getBuf() {
119: return buf;
120: }
121:
122: }
|