001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.logging.log4j.io.internal; 019 020import java.io.IOException; 021import java.io.OutputStream; 022import java.nio.charset.Charset; 023 024import org.apache.logging.log4j.Level; 025import org.apache.logging.log4j.Marker; 026import org.apache.logging.log4j.io.ByteStreamLogger; 027import org.apache.logging.log4j.spi.ExtendedLogger; 028 029/** 030 * Internal class that exists primarily to allow location calculation to work. 031 * 032 * @since 2.12 033 */ 034public class InternalOutputStream extends OutputStream { 035 036 private final ByteStreamLogger logger; 037 private final String fqcn; 038 039 public InternalOutputStream(final ExtendedLogger logger, final Level level, final Marker marker, 040 final Charset charset, final String fqcn) { 041 this.logger = new ByteStreamLogger(logger, level, marker, charset); 042 this.fqcn = fqcn; 043 } 044 045 @Override 046 public void close() throws IOException { 047 this.logger.close(this.fqcn); 048 } 049 050 @Override 051 public void flush() throws IOException { 052 // do nothing 053 } 054 055 @Override 056 public void write(final byte[] b) throws IOException { 057 this.logger.put(this.fqcn, b, 0, b.length); 058 } 059 060 @Override 061 public void write(final byte[] b, final int off, final int len) throws IOException { 062 this.logger.put(this.fqcn, b, off, len); 063 } 064 065 @Override 066 public void write(final int b) throws IOException { 067 this.logger.put(this.fqcn, (byte) (b & 0xFF)); 068 } 069}