/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. package org.hudsonci.utils.io;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.nio.channels.FileChannel;
  30. /**
  31. * Removes the beginning of a file.
  32. *
  33. * @author Jamie Whitehouse
  34. * @since 2.1.0
  35. */
  36. public class FileBeheader
  37. {
  38. /**
  39. * Beware, the original file is modified when chopping; you relinquish
  40. * all control of the original file.
  41. *
  42. * Requires write access to the same directory as the original.
  43. *
  44. * @param original the original file
  45. * @param tailToKeep the number of bytes to keep at the end of the file
  46. *
  47. * @return the original file chopped; for convenience
  48. *
  49. * @throws IllegalArgumentException if original is a directory
  50. * @throws IllegalStateException if some error occurs; it's unreasonable to try and recover from this
  51. */
  52. public static File chop( final File original, final long tailToKeep )
  53. {
  54. if ( !original.isFile() )
  55. {
  56. throw new IllegalArgumentException( "Cannot behead directories: " + original );
  57. }
  58. File truncatedFile = new File( original.getParentFile(), original.getName() + ".truncated" );
  59. FileChannel whole = null;
  60. FileChannel chopped = null;
  61. try
  62. {
  63. whole = new FileInputStream( original ).getChannel();
  64. chopped = new FileOutputStream( truncatedFile ).getChannel();
  65. long originalLength = original.length();
  66. long lengthToKeep = originalLength < tailToKeep ? 0 : originalLength - tailToKeep;
  67. whole.transferTo( lengthToKeep, originalLength, chopped );
  68. }
  69. catch ( IOException e )
  70. {
  71. throw new IllegalStateException( "Cannot behead file due to inconsistent internal state.", e );
  72. }
  73. finally
  74. {
  75. Closer.close( whole, chopped );
  76. }
  77. // Replace original with chopped file.
  78. boolean isRenamed = truncatedFile.renameTo( original );
  79. if ( !( isRenamed ) )
  80. {
  81. IllegalStateException renameException =
  82. new IllegalStateException( "Cannot replace original with chopped file to: " + original );
  83. String originalPath = original.getAbsolutePath();
  84. boolean isDeleted = original.delete();
  85. if ( isDeleted )
  86. {
  87. boolean isRenamedAgain = truncatedFile.renameTo( new File( originalPath ) );
  88. if ( !isRenamedAgain )
  89. {
  90. throw renameException;
  91. }
  92. }
  93. else
  94. {
  95. throw renameException;
  96. }
  97. }
  98. return original;
  99. }
  100. }