Skip to content

Commit

Permalink
#2415 Remove JDK 7 API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xylus committed Jan 5, 2017
1 parent 8acdf6f commit 5f736e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
6 changes: 0 additions & 6 deletions commons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
<packaging>jar</packaging>

<properties>
<jdk.version>1.7</jdk.version>
<jdk.home>${env.JAVA_7_HOME}</jdk.home>
<sniffer.artifactid>java17</sniffer.artifactid>

<!-- jdk.version 1.6 : for profiler-test compatibility -->
<!-- jdk.home 1.7 : for hbase compatibility -->
<!--
<jdk.version>1.6</jdk.version>
<jdk.home>${env.JAVA_7_HOME}</jdk.home>
<sniffer.artifactid>java16</sniffer.artifactid>
-->
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@
*/
public class BitCountingHeaderDecoder implements AgentStatHeaderDecoder {

private static final int NUM_BITS_PER_BYTE = 8;

private final BitSet headerBitSet;
private int position = 0;

public BitCountingHeaderDecoder(byte[] header) {
this.headerBitSet = BitSet.valueOf(header);
headerBitSet = new BitSet();
// strictly follows JDK 7's BitSet.valueOf(byte[])
for (int i = 0; i < header.length * NUM_BITS_PER_BYTE; i++) {
byte currentBits = header[i / NUM_BITS_PER_BYTE];
int bitMask = 1 << (i % NUM_BITS_PER_BYTE);
if ((currentBits & bitMask) > 0) {
headerBitSet.set(i);
}
}
// use below when using JDK 7+
// this.headerBitSet = BitSet.valueOf(header);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
public class BitCountingHeaderEncoder implements AgentStatHeaderEncoder {

private static final int NUM_BITS_PER_BYTE = 8;

private BitSet headerBitSet = new BitSet();
private int position = 0;

Expand All @@ -50,6 +52,19 @@ public void addCode(int code) {

@Override
public byte[] getHeader() {
return this.headerBitSet.toByteArray();
if (position == 0) {
return new byte[0];
}
// strictly follows JDK 7's BitSet.toByteArray()
int len = (headerBitSet.length() + (NUM_BITS_PER_BYTE - 1)) / NUM_BITS_PER_BYTE;
byte[] header = new byte[len];
for (int i = 0; i < len * NUM_BITS_PER_BYTE; ++i) {
int index = i / NUM_BITS_PER_BYTE;
int bitMask = (headerBitSet.get(i) ? 1 : 0) << (i % NUM_BITS_PER_BYTE);
header[index] |= bitMask;
}
return header;
// use below when using JDK 7+
// return this.headerBitSet.toByteArray();
}
}

0 comments on commit 5f736e0

Please # to comment.