ByteBufferAsLongBuffer.java revision e20e1b55eab0f2163e812a28cf3b3a682df38a1d
198aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski/*
298aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
398aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
498aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski *
598aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * This code is free software; you can redistribute it and/or modify it
698aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * under the terms of the GNU General Public License version 2 only, as
798aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * published by the Free Software Foundation.  Oracle designates this
898aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * particular file as subject to the "Classpath" exception as provided
998aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * by Oracle in the LICENSE file that accompanied this code.
1098aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski *
1198aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * This code is distributed in the hope that it will be useful, but WITHOUT
1298aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1398aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1498aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * version 2 for more details (a copy is included in the LICENSE file that
1598aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * accompanied this code).
1698aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski *
171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * You should have received a copy of the GNU General Public License version
181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * 2 along with this work; if not, write to the Free Software Foundation,
1998aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski *
21ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski * or visit www.oracle.com if you need additional information or have any
23d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski * questions.
24bf0bd0f9ac1ffa0231cff0f6591dede48b3c6d52Adam Lesinski */
251ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski
2698aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinskipackage java.nio;
2798aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski
2898aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinskiimport libcore.io.Memory;
29ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinski
30ceb9b2f80f853059233cdd29057f39a5960a74aeAdam Lesinskiclass ByteBufferAsLongBuffer extends LongBuffer {                 // package-private
31bf0bd0f9ac1ffa0231cff0f6591dede48b3c6d52Adam Lesinski
32cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    protected final ByteBuffer bb;
33cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    protected final int offset;
34cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    private final ByteOrder order;
35ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski
36bf0bd0f9ac1ffa0231cff0f6591dede48b3c6d52Adam Lesinski    ByteBufferAsLongBuffer(ByteBuffer bb, ByteOrder order) {
37ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski        super(-1, 0,
38bf0bd0f9ac1ffa0231cff0f6591dede48b3c6d52Adam Lesinski              bb.remaining() >> 3,
39cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski              bb.remaining() >> 3);
40d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski        this.bb = bb;
4198aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski        this.isReadOnly = bb.isReadOnly;
4298aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski        this.address = bb.address;
43cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski        this.order = order;
4498aa3ad6e46e3b0270785c8b3f9798e37e8af140Adam Lesinski        int cap = this.capacity();
451ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski        this.limit(cap);
46        int pos = this.position();
47        assert (pos <= cap);
48        offset = pos;
49    }
50
51    ByteBufferAsLongBuffer(ByteBuffer bb,
52                           int mark, int pos, int lim, int cap,
53                           int off, ByteOrder order) {
54        super(mark, pos, lim, cap);
55        this.bb = bb;
56        this.isReadOnly = bb.isReadOnly;
57        this.address = bb.address;
58        this.order = order;
59        offset = off;
60    }
61
62    public LongBuffer slice() {
63        int pos = this.position();
64        int lim = this.limit();
65        assert (pos <= lim);
66        int rem = (pos <= lim ? lim - pos : 0);
67        int off = (pos << 3) + offset;
68        assert (off >= 0);
69        return new ByteBufferAsLongBuffer(bb, -1, 0, rem, rem, off, order);
70    }
71
72    public LongBuffer duplicate() {
73        return new ByteBufferAsLongBuffer(bb,
74                                          this.markValue(),
75                                          this.position(),
76                                          this.limit(),
77                                          this.capacity(),
78                                          offset,
79                                          order);
80    }
81
82    public LongBuffer asReadOnlyBuffer() {
83        return new ByteBufferAsLongBuffer(bb.asReadOnlyBuffer(),
84                                          this.markValue(),
85                                          this.position(),
86                                          this.limit(),
87                                          this.capacity(),
88                                          offset,
89                                          order);
90    }
91
92    protected int ix(int i) {
93        return (i << 3) + offset;
94    }
95
96    public long get() {
97        return get(nextGetIndex());
98    }
99
100    public long get(int i) {
101        return bb.getLongUnchecked(ix(checkIndex(i)));
102    }
103
104    public LongBuffer put(long x) {
105        put(nextPutIndex(), x);
106        return this;
107    }
108
109    public LongBuffer put(int i, long x) {
110        if (isReadOnly) {
111            throw new ReadOnlyBufferException();
112        }
113        bb.putLongUnchecked(ix(checkIndex(i)), x);
114        return this;
115    }
116
117    public LongBuffer compact() {
118        if (isReadOnly) {
119            throw new ReadOnlyBufferException();
120        }
121        int pos = position();
122        int lim = limit();
123        assert (pos <= lim);
124        int rem = (pos <= lim ? lim - pos : 0);
125        if (!(bb instanceof DirectByteBuffer)) {
126            System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 3);
127        } else {
128            Memory.memmove(this, ix(0), this, ix(pos), rem << 3);
129        }
130        position(rem);
131        limit(capacity());
132        discardMark();
133        return this;
134    }
135
136    public boolean isDirect() {
137        return bb.isDirect();
138    }
139
140    public boolean isReadOnly() {
141        return isReadOnly;
142    }
143
144    public ByteOrder order() {
145        return order;
146    }
147}
148