PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-utils/src/main/java/org/hudsonci/utils/io/FileBeheader.java

http://github.com/hudson/hudson
Java | 110 lines | 56 code | 10 blank | 44 comment | 4 complexity | ccb0e267a220b211c7a6fd154a35caa5 MD5 | raw file
  1/**
  2 * The MIT License
  3 *
  4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24
 25package org.hudsonci.utils.io;
 26
 27import java.io.File;
 28import java.io.FileInputStream;
 29import java.io.FileOutputStream;
 30import java.io.IOException;
 31import java.nio.channels.FileChannel;
 32
 33/**
 34 * Removes the beginning of a file.
 35 * 
 36 * @author Jamie Whitehouse
 37 * @since 2.1.0
 38 */
 39public class FileBeheader
 40{
 41    /**
 42     * Beware, the original file is modified when chopping; you relinquish
 43     * all control of the original file.
 44     * 
 45     * Requires write access to the same directory as the original.
 46     * 
 47     * @param original the original file
 48     * @param tailToKeep the number of bytes to keep at the end of the file
 49     * 
 50     * @return the original file chopped; for convenience
 51     * 
 52     * @throws IllegalArgumentException if original is a directory
 53     * @throws IllegalStateException if some error occurs; it's unreasonable to try and recover from this
 54     */
 55    public static File chop( final File original, final long tailToKeep )
 56    {
 57        if ( !original.isFile() )
 58        {
 59            throw new IllegalArgumentException( "Cannot behead directories: " + original );
 60        }
 61
 62        File truncatedFile = new File( original.getParentFile(), original.getName() + ".truncated" );
 63
 64        FileChannel whole = null;
 65        FileChannel chopped = null;
 66        try
 67        {
 68            whole = new FileInputStream( original ).getChannel();
 69            chopped = new FileOutputStream( truncatedFile ).getChannel();
 70
 71            long originalLength = original.length();
 72            long lengthToKeep = originalLength < tailToKeep ? 0 : originalLength - tailToKeep;
 73            whole.transferTo( lengthToKeep, originalLength, chopped );
 74        }
 75        catch ( IOException e )
 76        {
 77            throw new IllegalStateException( "Cannot behead file due to inconsistent internal state.", e );
 78        }
 79        finally
 80        {
 81            Closer.close( whole, chopped );
 82        }
 83
 84        // Replace original with chopped file.
 85        boolean isRenamed = truncatedFile.renameTo( original );
 86        if ( !( isRenamed ) )
 87        {
 88            IllegalStateException renameException =
 89                new IllegalStateException( "Cannot replace original with chopped file to: " + original );
 90
 91            String originalPath = original.getAbsolutePath();
 92            boolean isDeleted = original.delete();
 93            if ( isDeleted )
 94            {
 95                boolean isRenamedAgain = truncatedFile.renameTo( new File( originalPath ) );
 96                if ( !isRenamedAgain )
 97                {
 98                    throw renameException;
 99                }
100            }
101            else
102            {
103                throw renameException;
104            }
105        }
106
107        return original;
108    }
109
110}