Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Mar 11, 2024
1 parent 1761057 commit e52f564
Show file tree
Hide file tree
Showing 8 changed files with 772 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/spring/csb9ea5b7a-df47-11ee-91f8-acde48001122.java
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;
}

}
70 changes: 70 additions & 0 deletions docs/spring/csba39874a-df47-11ee-91f8-acde48001122.java
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;
}
}
Loading

0 comments on commit e52f564

Please sign in to comment.