jdk/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4115 e09be02771b6
child 17922 d56eec572de5
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
     2
 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#warn This file is preprocessed before being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
class ByteBufferAs$Type$Buffer$RW$$BO$                  // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    protected final ByteBuffer bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) {   // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(-1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
              bb.remaining() >> $LG_BYTES_PER_VALUE$,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
              bb.remaining() >> $LG_BYTES_PER_VALUE$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.bb = bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // enforce limit == capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int cap = this.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.limit(cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int pos = this.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        assert (pos <= cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        offset = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                     int mark, int pos, int lim, int cap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                     int off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(mark, pos, lim, cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.bb = bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        offset = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        super(bb, mark, pos, lim, cap, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public $Type$Buffer slice() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int pos = this.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int lim = this.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int off = (pos << $LG_BYTES_PER_VALUE$) + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        assert (off >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public $Type$Buffer duplicate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                                    this.markValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                    this.position(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                                    this.limit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                    this.capacity(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                                    offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public $Type$Buffer asReadOnlyBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new ByteBufferAs$Type$BufferR$BO$(bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                 this.markValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                 this.position(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                                 this.limit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                                 this.capacity(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                 offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected int ix(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return (i << $LG_BYTES_PER_VALUE$) + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public $type$ get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return Bits.get$Type$$BO$(bb, ix(nextGetIndex()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public $type$ get(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return Bits.get$Type$$BO$(bb, ix(checkIndex(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public $Type$Buffer put($type$ x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Bits.put$Type$$BO$(bb, ix(nextPutIndex()), x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public $Type$Buffer put(int i, $type$ x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Bits.put$Type$$BO$(bb, ix(checkIndex(i)), x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public $Type$Buffer compact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ByteBuffer db = bb.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        db.limit(ix(lim));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        db.position(ix(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ByteBuffer sb = db.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        sb.position(pos << $LG_BYTES_PER_VALUE$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        sb.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        position(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        limit(capacity());
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 1247
diff changeset
   153
        discardMark();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public boolean isDirect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return bb.isDirect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean isReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return {#if[rw]?false:true};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public String toString(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if ((end > limit()) || (start > end))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int len = end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            char[] ca = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            CharBuffer cb = CharBuffer.wrap(ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            CharBuffer db = this.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            db.position(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            db.limit(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            cb.put(db);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return new String(ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (StringIndexOutOfBoundsException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // --- Methods to support CharSequence ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
1224
8c1bc7c5dd00 6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents: 2
diff changeset
   190
    public CharBuffer subSequence(int start, int end) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        pos = (pos <= lim ? pos : lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int len = lim - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if ((start < 0) || (end > len) || (start > end))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new IndexOutOfBoundsException();
2593
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   199
        return new ByteBufferAsCharBuffer$RW$$BO$(bb,
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   200
                                                  -1,
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   201
                                                  pos + start,
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   202
                                                  pos + end,
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   203
                                                  capacity(),
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1634
diff changeset
   204
                                                  offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public ByteOrder order() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
#if[boB]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return ByteOrder.BIG_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
#end[boB]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
#if[boL]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return ByteOrder.LITTLE_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
#end[boL]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}