Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2009, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.logmanager.handlers;
 
 import java.io.File;
 
 
 public class SizeRotatingFileHandler extends FileHandler {
     // by default, rotate at 10MB
     private long rotateSize = 0xa0000L;
     private long currentSize;
     private int maxBackupIndex = 1;

    
Construct a new instance with no formatter and no output file.
 
     public SizeRotatingFileHandler() {
     }

    
Construct a new instance with the given output file.

Parameters:
file the file
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public SizeRotatingFileHandler(final File filethrows FileNotFoundException {
         super(file);
     }

    
Construct a new instance with the given output file and append setting.

Parameters:
file the file
append true to append, false to overwrite
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public SizeRotatingFileHandler(final File filefinal boolean appendthrows FileNotFoundException {
         super(fileappend);
     }

    
Construct a new instance with the given output file.

Parameters:
fileName the file name
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public SizeRotatingFileHandler(final String fileNamethrows FileNotFoundException {
         super(fileName);
     }

    
Construct a new instance with the given output file and append setting.

Parameters:
fileName the file name
append true to append, false to overwrite
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public SizeRotatingFileHandler(final String fileNamefinal boolean appendthrows FileNotFoundException {
         super(fileNameappend);
     }

    
Construct a new instance with no formatter and no output file.
 
     public SizeRotatingFileHandler(final long rotateSizefinal int maxBackupIndex) {
         this. = rotateSize;
         this. = maxBackupIndex;
     }

    
Construct a new instance with the given output file.

Parameters:
file the file
Throws:
java.io.FileNotFoundException if the file could not be found on open
    public SizeRotatingFileHandler(final File filefinal long rotateSizefinal int maxBackupIndexthrows FileNotFoundException {
        super(file);
        this. = rotateSize;
        this. = maxBackupIndex;
    }

    
Construct a new instance with the given output file and append setting.

Parameters:
file the file
append true to append, false to overwrite
Throws:
java.io.FileNotFoundException if the file could not be found on open
    public SizeRotatingFileHandler(final File filefinal boolean appendfinal long rotateSizefinal int maxBackupIndexthrows FileNotFoundException {
        super(fileappend);
        this. = rotateSize;
        this. = maxBackupIndex;
    }

    
    public void setOutputStream(final OutputStream outputStream) {
        super.setOutputStream(outputStream == null ? null : new CountingOutputStream(outputStream));
    }

    
    public void setFile(final File filethrows FileNotFoundException {
        synchronized () {
            super.setFile(file);
             = file == null ? 0L : file.length();
        }
    }

    
Set the rotation size, in bytes.

Parameters:
rotateSize the number of bytes before the log is rotated
    public void setRotateSize(final long rotateSize) {
        checkAccess();
        synchronized () {
            this. = rotateSize;
        }
    }

    
Set the maximum backup index (the number of log files to keep around).

Parameters:
maxBackupIndex the maximum backup index
    public void setMaxBackupIndex(final int maxBackupIndex) {
        checkAccess();
        synchronized () {
            this. = maxBackupIndex;
        }
    }

    
    protected void preWrite(final ExtLogRecord record) {
        final int maxBackupIndex = this.;
        if ( >  && maxBackupIndex > 0) {
            try {
                final File file = getFile();
                if (file == null) {
                    // no file is set; a direct output stream or writer was specified
                    return;
                }
                // close the old file.
                setFile(null);
                // rotate.  First, drop the max file (if any), then move each file to the next higher slot.
                new File(file.getAbsolutePath() + "." + maxBackupIndex).delete();
                for (int i = maxBackupIndex - 1; i > 1; i--) {
                    new File(file.getAbsolutePath() + "." + i).renameTo(new File(file.getAbsolutePath() + "." + (i - 1)));
                }
                file.renameTo(new File(file.getAbsolutePath() + ".1"));
                // start with new file.
                setFile(file);
            } catch (FileNotFoundException e) {
                reportError("Unable to rotate log file"e.);
            }
        }
    }
    private final class CountingOutputStream extends OutputStream {
        private final OutputStream delegate;
        private CountingOutputStream(final OutputStream delegate) {
            this. = delegate;
        }
        public void write(final int bthrows IOException {
            .write(b);
             ++;
        }
        public void write(final byte[] bthrows IOException {
            .write(b);
             += b.length;
        }
        public void write(final byte[] bfinal int offfinal int lenthrows IOException {
            .write(bofflen);
             += len;
        }
        public void flush() throws IOException {
            .flush();
        }
        public void close() throws IOException {
            .close();
        }
    }
New to GrepCode? Check out our FAQ X