[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.fs; |
---|
| 19 | |
---|
| 20 | import java.io.*; |
---|
| 21 | |
---|
| 22 | /** Utility that wraps a {@link OutputStream} in a {@link DataOutputStream}, |
---|
| 23 | * buffers output through a {@link BufferedOutputStream} and creates a checksum |
---|
| 24 | * file. */ |
---|
| 25 | public class FSDataOutputStream extends DataOutputStream implements Syncable { |
---|
| 26 | private OutputStream wrappedStream; |
---|
| 27 | |
---|
| 28 | private static class PositionCache extends FilterOutputStream { |
---|
| 29 | private FileSystem.Statistics statistics; |
---|
| 30 | long position; |
---|
| 31 | |
---|
| 32 | public PositionCache(OutputStream out, |
---|
| 33 | FileSystem.Statistics stats, |
---|
| 34 | long pos) throws IOException { |
---|
| 35 | super(out); |
---|
| 36 | statistics = stats; |
---|
| 37 | position = pos; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public void write(int b) throws IOException { |
---|
| 41 | out.write(b); |
---|
| 42 | position++; |
---|
| 43 | if (statistics != null) { |
---|
| 44 | statistics.incrementBytesWritten(1); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public void write(byte b[], int off, int len) throws IOException { |
---|
| 49 | out.write(b, off, len); |
---|
| 50 | position += len; // update position |
---|
| 51 | if (statistics != null) { |
---|
| 52 | statistics.incrementBytesWritten(len); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | public long getPos() throws IOException { |
---|
| 57 | return position; // return cached position |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | public void close() throws IOException { |
---|
| 61 | out.close(); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | @Deprecated |
---|
| 66 | public FSDataOutputStream(OutputStream out) throws IOException { |
---|
| 67 | this(out, null); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public FSDataOutputStream(OutputStream out, FileSystem.Statistics stats) |
---|
| 71 | throws IOException { |
---|
| 72 | this(out, stats, 0); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public FSDataOutputStream(OutputStream out, FileSystem.Statistics stats, |
---|
| 76 | long startPosition) throws IOException { |
---|
| 77 | super(new PositionCache(out, stats, startPosition)); |
---|
| 78 | wrappedStream = out; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public long getPos() throws IOException { |
---|
| 82 | return ((PositionCache)out).getPos(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public void close() throws IOException { |
---|
| 86 | out.close(); // This invokes PositionCache.close() |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | // Returns the underlying output stream. This is used by unit tests. |
---|
| 90 | public OutputStream getWrappedStream() { |
---|
| 91 | return wrappedStream; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** {@inheritDoc} */ |
---|
| 95 | public void sync() throws IOException { |
---|
| 96 | if (wrappedStream instanceof Syncable) { |
---|
| 97 | ((Syncable)wrappedStream).sync(); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | } |
---|