-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
772 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.huifer.kafka.application.ttl; | ||
|
||
/** | ||
* <p>Title : BytesUtils </p> | ||
* <p>Description : </p> | ||
* | ||
* @author huifer | ||
* @date 2019-06-27 | ||
*/ | ||
public class BytesUtils { | ||
|
||
public static void main(String[] args) { | ||
long i = 22L; | ||
byte[] bytes = long2byte(i); | ||
long l = byte2long(bytes); | ||
System.out.println(l); | ||
|
||
int ii = 10; | ||
byte[] bytes1 = int2byte(ii); | ||
int i1 = byte2int(bytes1); | ||
System.out.println(i1); | ||
} | ||
|
||
public static byte[] long2byte(Long ms) { | ||
byte[] buffer = new byte[8]; | ||
for (int i = 0; i < 8; i++) { | ||
int offset = 64 - (i + 1) * 8; | ||
buffer[i] = (byte) ((ms >> offset) & 0xff); | ||
} | ||
return buffer; | ||
} | ||
|
||
public static long byte2long(byte[] bytes) { | ||
long value = 0; | ||
for (int i = 0; i < 8; i++) { | ||
value <<= 8; | ||
value |= (bytes[i] & 0xff); | ||
} | ||
return value; | ||
} | ||
|
||
public static int byte2int(byte[] bytes) { | ||
return (bytes[0] & 0xff) << 24 | ||
| (bytes[1] & 0xff) << 16 | ||
| (bytes[2] & 0xff) << 8 | ||
| (bytes[3] & 0xff); | ||
} | ||
|
||
public static byte[] int2byte(int num) { | ||
byte[] bytes = new byte[4]; | ||
bytes[0] = (byte) ((num >> 24) & 0xff); | ||
bytes[1] = (byte) ((num >> 16) & 0xff); | ||
bytes[2] = (byte) ((num >> 8) & 0xff); | ||
bytes[3] = (byte) (num & 0xff); | ||
return bytes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.huifer.idgen.my.service.conv; | ||
|
||
import com.huifer.idgen.my.service.bean.Id; | ||
import com.huifer.idgen.my.service.bean.IdMeta; | ||
import com.huifer.idgen.my.service.bean.enums.IdType; | ||
import com.huifer.idgen.my.service.factory.IdMetaFactory; | ||
|
||
/** | ||
* @author: wang | ||
* @description: | ||
*/ | ||
public class IdConverterImpl implements IdConverter { | ||
|
||
private IdMeta idMeta; | ||
|
||
public IdConverterImpl(IdType idType) { | ||
this(IdMetaFactory.getIdMeta(idType)); | ||
} | ||
|
||
public IdConverterImpl(IdMeta idMeta) { | ||
this.idMeta = idMeta; | ||
} | ||
|
||
|
||
@Override | ||
public long converter(Id id) { | ||
return doConverter(id, this.idMeta); | ||
} | ||
|
||
@Override | ||
public Id converter(long id) { | ||
return doConvert(id, idMeta); | ||
} | ||
|
||
/** | ||
* 根据id生产,根据id元数据生产一个long类型的id | ||
* | ||
* @param id | ||
* @param idMeta | ||
* @return | ||
*/ | ||
protected long doConverter(Id id, IdMeta idMeta) { | ||
long res = 0; | ||
res |= id.getMachine(); | ||
res |= id.getSeq() << idMeta.getMachineBits(); | ||
res |= id.getTime() << idMeta.getTimeBitsStartPos(); | ||
res |= id.getGenMethod() << idMeta.getGenMethodBitsStartPos(); | ||
res |= id.getType() << idMeta.getTypeBitsStartPos(); | ||
res |= id.getVersion() << idMeta.getVersionBitsStartPos(); | ||
return res; | ||
} | ||
|
||
/** | ||
* 根据long类型id,根据id元数据返回这个id的意义 | ||
* | ||
* @param id | ||
* @param idMeta | ||
* @return | ||
*/ | ||
protected Id doConvert(long id, IdMeta idMeta) { | ||
Id res = new Id(); | ||
res.setMachine(id & idMeta.getMachineBitsMask()); | ||
res.setSeq((id >>> idMeta.getMachineBits()) & idMeta.getSeqBitsMask()); | ||
res.setTime((id >>> idMeta.getTimeBitsStartPos()) & idMeta.getTimeBitsMask()); | ||
res.setGenMethod((id >>> idMeta.getGenMethodBitsStartPos()) & idMeta.getGenMethodBitsMask()); | ||
res.setType((id >>> idMeta.getTypeBitsStartPos()) & idMeta.getTypeBitsMask()); | ||
res.setVersion((id >>> idMeta.getVersionBitsStartPos()) & idMeta.getVersionBitsMask()); | ||
return res; | ||
} | ||
} |
Oops, something went wrong.