1 /*
2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.nio;
27
28
29 // ## If the sequence is a string, use reflection to share its array
30
31 class StringCharBuffer // package-private
32 extends CharBuffer
33 {
34 CharSequence str;
35
36 StringCharBuffer(CharSequence s, int start, int end) { // package-private
37 super(-1, start, end, s.length());
38 int n = s.length();
39 if ((start < 0) || (start > n) || (end < start) || (end > n))
40 throw new IndexOutOfBoundsException();
41 str = s;
42 this.isReadOnly = true;
43 }
44
45 public CharBuffer slice() {
46 return new StringCharBuffer(str,
47 -1,
48 0,
49 this.remaining(),
50 this.remaining(),
51 offset + this.position());
52 }
53
54 private StringCharBuffer(CharSequence s,
55 int mark,
56 int pos,
57 int limit,
58 int cap,
59 int offset) {
60 super(mark, pos, limit, cap, null, offset);
61 str = s;
62 this.isReadOnly = true;
63 }
64
65 public CharBuffer duplicate() {
66 return new StringCharBuffer(str, markValue(),
67 position(), limit(), capacity(), offset);
68 }
69
70 public CharBuffer asReadOnlyBuffer() {
71 return duplicate();
72 }
73
|
1 /*
2 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.nio;
27
28 import java.util.Objects;
29
30 // ## If the sequence is a string, use reflection to share its array
31
32 class StringCharBuffer // package-private
33 extends CharBuffer
34 {
35 CharSequence str;
36
37 StringCharBuffer(CharSequence s, int start, int end) { // package-private
38 super(-1, start, end, s.length());
39 int n = s.length();
40 if ((start < 0) || (start > n) || (end < start) || (end > n))
41 throw new IndexOutOfBoundsException();
42 str = s;
43 this.isReadOnly = true;
44 }
45
46 public CharBuffer slice() {
47 return new StringCharBuffer(str,
48 -1,
49 0,
50 this.remaining(),
51 this.remaining(),
52 offset + this.position());
53 }
54
55 @Override
56 public CharBuffer slice(int index, int length) {
57 Objects.checkFromIndexSize(index, length, limit());
58 return new StringCharBuffer(str,
59 -1,
60 0,
61 length,
62 length,
63 offset + index);
64 }
65
66 private StringCharBuffer(CharSequence s,
67 int mark,
68 int pos,
69 int limit,
70 int cap,
71 int offset) {
72 super(mark, pos, limit, cap, null, offset);
73 str = s;
74 this.isReadOnly = true;
75 }
76
77 public CharBuffer duplicate() {
78 return new StringCharBuffer(str, markValue(),
79 position(), limit(), capacity(), offset);
80 }
81
82 public CharBuffer asReadOnlyBuffer() {
83 return duplicate();
84 }
85
|