Skip to content

Commit 7adf0c1

Browse files
committed
Merge pull request xerial#83 from xerial/outputstream-opt
Optimization of SnappyOutputStream
2 parents f762770 + dbc610e commit 7adf0c1

File tree

6 files changed

+201
-113
lines changed

6 files changed

+201
-113
lines changed

build.sbt

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ pomExtra := {
4747
</scm>
4848
}
4949

50+
scalaVersion := "2.11.1"
51+
5052
javacOptions in (Compile, compile) ++= Seq("-encoding", "UTF-8", "-Xlint:unchecked", "-Xlint:deprecation", "-source", "1.6", "-target", "1.6")
5153

5254
testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v")
5355

54-
//concurrentRestrictions in Global := Seq(Tags.limit(Tags.Test, 1))
56+
concurrentRestrictions in Global := Seq(Tags.limit(Tags.Test, 1))
5557

5658
autoScalaLibrary := false
5759

@@ -64,7 +66,9 @@ incOptions := incOptions.value.withNameHashing(true)
6466
libraryDependencies ++= Seq(
6567
"junit" % "junit" % "4.8.2" % "test",
6668
"org.codehaus.plexus" % "plexus-classworlds" % "2.4" % "test",
67-
"org.xerial" % "xerial-core" % "1.0.21" % "test",
69+
"org.xerial.java" % "xerial-core" % "2.1" % "test",
70+
"org.xerial" % "xerial-core" % "3.2.3" % "test",
71+
"org.scalatest" % "scalatest_2.11" % "2.2.0" % "test",
6872
"org.osgi" % "org.osgi.core" % "4.3.0" % "provided",
6973
"com.novocode" % "junit-interface" % "0.10" % "test"
7074
)

src/main/java/org/xerial/snappy/SnappyCodec.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,34 @@
5050
public class SnappyCodec
5151
{
5252
public static final byte[] MAGIC_HEADER = new byte[] { -126, 'S', 'N', 'A', 'P', 'P', 'Y', 0 };
53-
public static final int MAGIC_LEN = 8;
53+
public static final int MAGIC_LEN = MAGIC_HEADER.length;
54+
public static final int HEADER_SIZE = MAGIC_LEN + 8;
5455

5556
public static final int DEFAULT_VERSION = 1;
5657
public static final int MINIMUM_COMPATIBLE_VERSION = 1;
5758

5859
public final byte[] magic;
5960
public final int version;
6061
public final int compatibleVersion;
62+
private final byte[] headerArray;
6163

6264
private SnappyCodec(byte[] magic, int version, int compatibleVersion) {
6365
this.magic = magic;
6466
this.version = version;
6567
this.compatibleVersion = compatibleVersion;
68+
69+
ByteArrayOutputStream header = new ByteArrayOutputStream(HEADER_SIZE);
70+
DataOutputStream d = new DataOutputStream(header);
71+
try {
72+
d.write(magic, 0, MAGIC_LEN);
73+
d.writeInt(version);
74+
d.writeInt(compatibleVersion);
75+
d.close();
76+
}
77+
catch(IOException e) {
78+
throw new RuntimeException(e);
79+
}
80+
headerArray = header.toByteArray();
6681
}
6782

6883
@Override
@@ -71,17 +86,17 @@ public String toString() {
7186
}
7287

7388
public static int headerSize() {
74-
return MAGIC_LEN + 4 * 2;
89+
return HEADER_SIZE;
7590
}
7691

77-
public void writeHeader(OutputStream out) throws IOException {
78-
ByteArrayOutputStream header = new ByteArrayOutputStream();
79-
DataOutputStream d = new DataOutputStream(header);
80-
d.write(magic, 0, MAGIC_LEN);
81-
d.writeInt(version);
82-
d.writeInt(compatibleVersion);
83-
d.close();
84-
out.write(header.toByteArray(), 0, header.size());
92+
public int writeHeader(byte[] dst, int dstOffset) {
93+
System.arraycopy(headerArray, 0, dst, dstOffset, headerArray.length);
94+
return headerArray.length;
95+
}
96+
97+
public int writeHeader(OutputStream out) throws IOException {
98+
out.write(headerArray, 0, headerArray.length);
99+
return headerArray.length;
85100
}
86101

87102
public boolean isValidMagicHeader() {
@@ -97,8 +112,6 @@ public static SnappyCodec readHeader(InputStream in) throws IOException {
97112
return new SnappyCodec(magic, version, compatibleVersion);
98113
}
99114

100-
public static SnappyCodec currentHeader() {
101-
return new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
102-
}
115+
public static SnappyCodec currentHeader = new SnappyCodec(MAGIC_HEADER, DEFAULT_VERSION, MINIMUM_COMPATIBLE_VERSION);
103116

104117
}

0 commit comments

Comments
 (0)