aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/ByteWriter.java
blob: 4bca8ad51f7586074a02e1114bf6e61e2fa3fadc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import com.yahoo.text.Utf8;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.CharsetEncoder;

/**
 * A buffered writer which accepts byte arrays in addition to character arrays.
 *
 * @author Steinar Knutsen
 */
public class ByteWriter extends AbstractByteWriter {

    private final OutputStream stream;

    public ByteWriter(final OutputStream stream, final CharsetEncoder encoder) {
        super(encoder);
        this.stream = stream;
    }
    public ByteWriter(final OutputStream stream) {
        super(Utf8.getNewEncoder());
        this.stream = stream;
    }

    @Override
    public void send(final ByteBuffer b) throws IOException {
        // we know from how BufferChain works we have a backing array
        stream.write(b.array(), b.position() + b.arrayOffset(), b.remaining());
    }

    @Override
    public void close() throws java.io.IOException {
        buffer.flush();
        // Unit tests in prelude depends on the stream _not_ being flushed, it
        // is necessary for Jetty to write content length headers, it seems.
        // stream.flush();
        stream.close();
    }

    @Override
    public void flush() throws IOException {
        buffer.flush();
        // Unit tests in prelude depends on the stream _not_ being flushed, it
        // is necessary for Jetty to write content length headers, it seems.
        // stream.flush();
    }
}