< prev index next >

src/java.base/share/classes/java/nio/X-Buffer.java.template

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -27,10 +27,11 @@
 
 package java.nio;
 
 #if[char]
 import java.io.IOException;
+import java.util.Objects;
 #end[char]
 #if[streamableType]
 import java.util.Spliterator;
 import java.util.stream.StreamSupport;
 import java.util.stream.$Streamtype$Stream;

@@ -1546,10 +1547,44 @@
      */
     public $Type$Buffer append($type$ $x$) {
         return put($x$);
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * @implSpec
+     * The implementation transfers data by one of two means. If the given {@link Appendable}
+     * is a {@link CharBuffer}, {@code put(CharBuffer)} is invoked on it with this
+     * buffer as parameter. Otherwise {@link Appendable#append(CharSequence, int, int)} is
+     * invoked on it with this buffer and its position and length as parameters.
+     *
+     * @param  out the appendable, non-null
+     * @return the number of characters transferred
+     * @throws IOException if an I/O error occurs when reading or writing
+     * @throws NullPointerException if {@code out} is {@code null}
+     * @throws BufferOverflowException if there is insufficient space in out
+     *         buffer for the remaining chars in the source buffer
+     * @throws IllegalArgumentException if out is this buffer
+     * @throws ReadOnlyBufferException if out is a read-only buffer
+     *
+     * @since 10
+     */
+    @Override
+    public long transferTo(Appendable out) throws IOException {
+        Objects.requireNonNull(out, "out");
+        if (this == out) {
+            throw new IllegalArgumentException("Illegal transfer of a buffer to itself");
+        }
+        int length = remaining();
+        if (out instanceof CharBuffer) {
+            ((CharBuffer)out).put(this);
+        } else {
+            out.append(this, position(), length);
+        }
+        return length;
+    }
 #end[char]
 
 
     // -- Other byte stuff: Access to binary data --
 
< prev index next >