50
50
public class SnappyCodec
51
51
{
52
52
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 ;
54
55
55
56
public static final int DEFAULT_VERSION = 1 ;
56
57
public static final int MINIMUM_COMPATIBLE_VERSION = 1 ;
57
58
58
59
public final byte [] magic ;
59
60
public final int version ;
60
61
public final int compatibleVersion ;
62
+ private final byte [] headerArray ;
61
63
62
64
private SnappyCodec (byte [] magic , int version , int compatibleVersion ) {
63
65
this .magic = magic ;
64
66
this .version = version ;
65
67
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 ();
66
81
}
67
82
68
83
@ Override
@@ -71,17 +86,17 @@ public String toString() {
71
86
}
72
87
73
88
public static int headerSize () {
74
- return MAGIC_LEN + 4 * 2 ;
89
+ return HEADER_SIZE ;
75
90
}
76
91
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 ;
85
100
}
86
101
87
102
public boolean isValidMagicHeader () {
@@ -97,8 +112,6 @@ public static SnappyCodec readHeader(InputStream in) throws IOException {
97
112
return new SnappyCodec (magic , version , compatibleVersion );
98
113
}
99
114
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 );
103
116
104
117
}
0 commit comments