// Copyright 2016 The Domain Registry Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.domain.registry.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.annotation.OverridingMethodsMustInvokeSuper; import javax.annotation.WillCloseWhenClosed; import javax.annotation.concurrent.NotThreadSafe; /** * {@link OutputStream} wrapper that offers some additional magic. * *
This method may not be overridden, use {@link #onClose()} instead. * * @see java.io.OutputStream#close() */ @Override @NonFinalForTesting public void close() throws IOException { if (out == null) { return; } try { flush(); } catch (IOException e) { logger.warningfmt(e, "%s.flush() failed", getClass().getSimpleName()); } logger.infofmt("%s closed with %,d bytes written", getClass().getSimpleName(), count); if (expected != -1 && count != expected) { throw new IOException(String.format( "%s expected %,d bytes but got %,d bytes", getClass().getSimpleName(), expected, count)); } onClose(); if (shouldClose) { out.close(); } out = null; } /** * Overridable method that's called by {@link #close()}. * *
This method does nothing by default. * * @throws IOException */ protected void onClose() throws IOException { // Does nothing by default. } }