Skip to content

Commit 475505c

Browse files
committed
PLXCOMP-282: sketch in snappy
1 parent 4b766e4 commit 475505c

14 files changed

+608
-5
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
<artifactId>commons-compress</artifactId>
6464
<version>1.9</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>org.xerial.snappy</groupId>
68+
<artifactId>snappy-java</artifactId>
69+
<version>1.1.1.6</version>
70+
</dependency>
6671
<dependency>
6772
<groupId>junit</groupId>
6873
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.codehaus.plexus.archiver.snappy;
2+
3+
import org.codehaus.plexus.components.io.attributes.Java7FileAttributes;
4+
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
5+
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;
6+
import org.codehaus.plexus.util.IOUtil;
7+
8+
import javax.annotation.Nonnull;
9+
import javax.annotation.WillNotClose;
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.util.HashMap;
15+
16+
17+
/**
18+
* Implementation of {@link org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection} for
19+
* snappy compressed files.
20+
*/
21+
public class PlexusIoSnappyResourceCollection
22+
extends PlexusIoCompressedFileResourceCollection
23+
{
24+
@Nonnull
25+
protected @WillNotClose InputStream getInputStream( File file )
26+
throws IOException
27+
{
28+
InputStream fis = new FileInputStream( file );
29+
try
30+
{
31+
final InputStream result = SnappyUnArchiver.getSnappyInputStream( fis );
32+
fis = null;
33+
return result;
34+
}
35+
finally
36+
{
37+
IOUtil.close( fis );
38+
}
39+
}
40+
41+
42+
@Override protected PlexusIoResourceAttributes getAttributes(File file) throws IOException {
43+
return new Java7FileAttributes(file, new HashMap<Integer, String>(), new HashMap<Integer, String>());
44+
}
45+
46+
protected String getDefaultExtension()
47+
{
48+
return ".snappy";
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.codehaus.plexus.archiver.snappy;
2+
3+
/**
4+
*
5+
* Copyright 2004 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import org.codehaus.plexus.archiver.AbstractArchiver;
21+
import org.codehaus.plexus.archiver.ArchiveEntry;
22+
import org.codehaus.plexus.archiver.ArchiverException;
23+
import org.codehaus.plexus.archiver.ResourceIterator;
24+
import org.codehaus.plexus.archiver.bzip2.BZip2Compressor;
25+
26+
import java.io.IOException;
27+
28+
/**
29+
* Snappy archiver.
30+
*/
31+
public class SnappyArchiver
32+
extends AbstractArchiver
33+
{
34+
private SnappyCompressor compressor = new SnappyCompressor();
35+
36+
public void execute()
37+
throws ArchiverException, IOException
38+
{
39+
if ( !checkForced() )
40+
{
41+
return;
42+
}
43+
44+
ResourceIterator iter = getResources();
45+
ArchiveEntry entry = iter.next();
46+
if ( iter.hasNext() )
47+
{
48+
throw new ArchiverException( "There is more than one file in input." );
49+
}
50+
compressor.setSource( entry.getResource() );
51+
compressor.setDestFile( getDestFile() );
52+
compressor.compress();
53+
}
54+
55+
public boolean isSupportingForced()
56+
{
57+
return true;
58+
}
59+
60+
protected void close()
61+
{
62+
compressor.close();
63+
}
64+
65+
protected String getArchiveType()
66+
{
67+
return "snappy";
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.codehaus.plexus.archiver.snappy;
2+
3+
/**
4+
*
5+
* Copyright 2004 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import org.codehaus.plexus.archiver.ArchiverException;
21+
import org.codehaus.plexus.archiver.util.Compressor;
22+
import org.codehaus.plexus.util.IOUtil;
23+
import org.xerial.snappy.SnappyOutputStream;
24+
25+
import java.io.IOException;
26+
27+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
28+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
29+
30+
/**
31+
* Snappy compression
32+
*/
33+
public class SnappyCompressor
34+
extends Compressor
35+
{
36+
private SnappyOutputStream zOut;
37+
38+
/**
39+
* perform the Snappy compression operation.
40+
*/
41+
public void compress()
42+
throws ArchiverException
43+
{
44+
try
45+
{
46+
zOut = new SnappyOutputStream( bufferedOutputStream( fileOutputStream( getDestFile() ) ) );
47+
compress( getSource(), zOut );
48+
}
49+
catch ( IOException ioe )
50+
{
51+
String msg = "Problem creating snappy " + ioe.getMessage();
52+
throw new ArchiverException( msg, ioe );
53+
}
54+
}
55+
56+
public void close()
57+
{
58+
IOUtil.close( zOut );
59+
zOut = null;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.codehaus.plexus.archiver.snappy;
2+
3+
/**
4+
*
5+
* Copyright 2004 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import org.codehaus.plexus.archiver.AbstractUnArchiver;
21+
import org.codehaus.plexus.archiver.ArchiverException;
22+
import org.xerial.snappy.SnappyInputStream;
23+
24+
import javax.annotation.Nonnull;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
29+
import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
30+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
31+
import static org.codehaus.plexus.archiver.util.Streams.copyFully;
32+
import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
33+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
34+
35+
/**
36+
* Unarchiver for snappy-compressed files.
37+
*/
38+
public class SnappyUnArchiver
39+
extends AbstractUnArchiver
40+
{
41+
private final static String OPERATION_SNAPPY = "snappy";
42+
43+
public SnappyUnArchiver()
44+
{
45+
}
46+
47+
public SnappyUnArchiver(File sourceFile)
48+
{
49+
super( sourceFile );
50+
}
51+
52+
protected void execute()
53+
throws ArchiverException
54+
{
55+
if ( getSourceFile().lastModified() > getDestFile().lastModified() )
56+
{
57+
getLogger().info(
58+
"Expanding " + getSourceFile().getAbsolutePath() + " to " + getDestFile().getAbsolutePath() );
59+
60+
copyFully( getSnappyInputStream(bufferedInputStream(fileInputStream(getSourceFile(), OPERATION_SNAPPY))),
61+
bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_SNAPPY) ), OPERATION_SNAPPY);
62+
}
63+
}
64+
65+
public static
66+
@Nonnull
67+
SnappyInputStream getSnappyInputStream( InputStream bis )
68+
throws ArchiverException
69+
{
70+
try
71+
{
72+
return new SnappyInputStream( bis );
73+
}
74+
catch ( IOException e )
75+
{
76+
throw new ArchiverException( "Trouble creating Snappy compressor, invalid file ?", e );
77+
}
78+
}
79+
80+
protected void execute( String path, File outputDirectory )
81+
{
82+
throw new UnsupportedOperationException( "Targeted extraction not supported in Snappy format." );
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.codehaus.plexus.archiver.tar;
2+
3+
import java.io.File;
4+
5+
public class PlexusIoTarSnappyFileResourceCollection
6+
extends PlexusIoTarFileResourceCollection
7+
{
8+
9+
protected TarFile newTarFile( File file )
10+
{
11+
return new SnappyTarFile( file );
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.codehaus.plexus.archiver.tar;
2+
3+
import org.codehaus.plexus.archiver.bzip2.BZip2UnArchiver;
4+
import org.codehaus.plexus.archiver.snappy.SnappyUnArchiver;
5+
import org.xerial.snappy.SnappyInputStream;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.zip.GZIPInputStream;
11+
12+
13+
/**
14+
* Extension of {@link org.codehaus.plexus.archiver.tar.TarFile} for snappy compressed files.
15+
*/
16+
public class SnappyTarFile extends TarFile
17+
{
18+
/**
19+
* Creates a new instance with the given file.
20+
*/
21+
public SnappyTarFile(File file)
22+
{
23+
super( file );
24+
}
25+
26+
protected InputStream getInputStream( File file )
27+
throws IOException
28+
{
29+
return SnappyUnArchiver.getSnappyInputStream( super.getInputStream( file ));
30+
}
31+
}

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
3232
import org.codehaus.plexus.util.IOUtil;
3333
import org.codehaus.plexus.util.StringUtils;
34+
import org.xerial.snappy.SnappyOutputStream;
3435

3536
import java.io.File;
3637
import java.io.FileOutputStream;
@@ -177,7 +178,7 @@ else if ( longFileMode.isFailMode() || longFileMode.isOmitMode() )
177178
while ( iter.hasNext() )
178179
{
179180
ArchiveEntry entry = iter.next();
180-
// Check if we don't add tar file in inself
181+
// Check if we don't add tar file in itself
181182
if ( ResourceUtils.isSame( entry.getResource(), tarFile ) )
182183
{
183184
throw new ArchiverException( "A tar file cannot include itself." );
@@ -464,7 +465,7 @@ public void setPreserveLeadingSlashes( boolean preserveLeadingSlashes )
464465
*/
465466
public static enum TarCompressionMethod
466467
{
467-
none, gzip, bzip2
468+
none, gzip, bzip2, snappy
468469

469470
}
470471

@@ -479,6 +480,10 @@ else if ( TarCompressionMethod.bzip2.equals( tarCompressionMethod) )
479480
{
480481
return new BZip2CompressorOutputStream( ostream );
481482
}
483+
else if ( TarCompressionMethod.snappy.equals( tarCompressionMethod ))
484+
{
485+
return new SnappyOutputStream( ostream );
486+
}
482487
return ostream;
483488
}
484489

0 commit comments

Comments
 (0)