---
title: 'Endlich transferTo(…) im InputStream'
url: 'https://tutego.de/blog/javainsel/2014/12/endlich-transferto-im-inputstream'
markdown: 'https://tutego.de/blog/javainsel/2014/12/endlich-transferto-im-inputstream.md'
date: '2014-12-25'
description: 'http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/003295073abf zeigt uns in Java 9 endlich eine lang erwartete Methode: + /** + * Reads all bytes from this input stream and writes the bytes to the + * given output stream in the order that they are read. On return, this + * input stream wi…'
taxonomy:
  category:
    - 'Java 9'
---

# Endlich transferTo(…) im InputStream

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Endlich transferTo(…) im InputStream

25.12.2014 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

[http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/003295073abf](http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/003295073abf "http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/003295073abf") zeigt uns in Java 9 endlich eine lang erwartete Methode:

```java
+    /**
+     * Reads all bytes from this input stream and writes the bytes to the
+     * given output stream in the order that they are read. On return, this
+     * input stream will be at end of stream. This method does not close either
+     * stream.
+     * 
+     * This method may block indefinitely reading from the input stream, or
+     * writing to the output stream. The behavior for the case where the input
+     * and/or output stream is asynchronously closed, or the thread
+     * interrupted during the transfer, is highly input and output stream
+     * specific, and therefore not specified.
+     * 
+     * If an I/O error occurs reading from the input stream or writing to the
+     * output stream, then it may do so after some bytes have been read or
+     * written. Consequently the input stream may not be at end of stream and
+     * one, or both, streams may be in an inconsistent state. It is strongly
+     * recommended that both streams be promptly closed if an I/O error occurs.
+     *
+     * @param  out the output stream, non-null
+     * @return the number of bytes transferred
+     * @throws IOException if an I/O error occurs when reading or writing
+     * @throws NullPointerException if {@code out} is {@code null}
+     *
+     * @since 1.9
+     */
+    public long transferTo(OutputStream out) throws IOException {
+        Objects.requireNonNull(out, "out");
+        long transferred = 0;
+        byte[] buffer = new byte[TRANSFER_BUFFER_SIZE];
+        int read;
+        while ((read = this.read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
+            out.write(buffer, 0, read);
+            transferred += read;
+        }
+        return transferred;
+    }
```

Kategorien: [Java 9](https://tutego.de/blog/javainsel/category/java-9/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Java 9, Stand 2014](https://tutego.de/blog/javainsel/2014/12/java-9-stand-2014.md)
- Next: [Meinen Lesern alles Gute für das Jahr 2015](https://tutego.de/blog/javainsel/2014/12/meinen-lesern-alles-gute-fr-das-jahr-2015.md)
