congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
cn.sinjinsong.common.util
Code IndexAdd Tabnine to your IDE (free)

How to use cn.sinjinsong.common.util

Best Java code snippets using cn.sinjinsong.common.util (Showing top 19 results out of 315)

origin: songxinjianqwe/Chat

public static String formatLocalDateTime(Long dateTime){
  return toLocalDateTime(dateTime).format(dateTimeFormatter);
}

origin: songxinjianqwe/Chat

public static <T> T deserialize(byte[] data, Class<T> cls) {
  try {
    T message = cls.newInstance();
    Schema<T> schema = getSchema(cls);
    ProtostuffIOUtil.mergeFrom(data, message, schema);
    return message;
  } catch (Exception e) {
    throw new IllegalStateException(e.getMessage(), e);
  }
}

origin: songxinjianqwe/Chat

  private String formatMessage(String originalText, Response response) {
    ResponseHeader header = response.getHeader();
    StringBuilder sb = new StringBuilder();
    sb.append(originalText)
        .append(header.getSender())
        .append(": ")
        .append(new String(response.getBody(), charset))
        .append("    ")
        .append(DateTimeUtil.formatLocalDateTime(header.getTimestamp()))
        .append("\n");
    return sb.toString();
  }
}
origin: songxinjianqwe/Chat

@Override
public final void run() {
  try {
    info.getReceiver().write(ByteBuffer.wrap(ProtoStuffUtil.serialize(process())));
  } catch (IOException e) {
    e.printStackTrace();
    throw new TaskException(info);
  } catch (InterruptedException e) {
    e.printStackTrace();
    throw new TaskException(info);
  }
}
origin: songxinjianqwe/Chat

public void run() {
  try {
    while (connected) {
      int size = 0;
      selector.select();
      for (Iterator<SelectionKey> it = selector.selectedKeys().iterator(); it.hasNext(); ) {
        SelectionKey selectionKey = it.next();
        it.remove();
        if (selectionKey.isReadable()) {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          while ((size = clientChannel.read(buf)) > 0) {
            buf.flip();
            baos.write(buf.array(), 0, size);
            buf.clear();
          }
          byte[] bytes = baos.toByteArray();
          baos.close();
          Response response = ProtoStuffUtil.deserialize(bytes, Response.class);
          handleResponse(response);
        }
      }
    }
  } catch (IOException e) {
    JOptionPane.showMessageDialog(null, "服务器关闭,请重新尝试连接");
    isLogin = false;
  }
}
origin: songxinjianqwe/Chat

String path = JOptionPane.showInputDialog("请输入保存的文件路径");
byte[] buf = response.getBody();
FileUtil.save(path, buf);
if(path.endsWith("jpg")){
origin: songxinjianqwe/Chat

.timestamp(header.getTimestamp())
.build(),
ZipUtil.zipCompress(result,suffix));
origin: songxinjianqwe/Chat

private void login() {
  String username = JOptionPane.showInputDialog("请输入用户名");
  String password = JOptionPane.showInputDialog("请输入密码");
  Message message = new Message(
      MessageHeader.builder()
          .type(MessageType.LOGIN)
          .sender(username)
          .timestamp(System.currentTimeMillis())
          .build(), password.getBytes(charset));
  try {
    clientChannel.write(ByteBuffer.wrap(ProtoStuffUtil.serialize(message)));
  } catch (IOException e) {
    e.printStackTrace();
  }
  this.username = username;
}
origin: songxinjianqwe/Chat

  @Override
  public void handle(Message message, Selector server, SelectionKey client, BlockingQueue<Task> queue, AtomicInteger onlineUsers) throws InterruptedException {
    TaskDescription taskDescription = ProtoStuffUtil.deserialize(message.getBody(), TaskDescription.class);
    Task task = new Task((SocketChannel) client.channel(), taskDescription.getType(), taskDescription.getDesc(), message);
    try {
      queue.put(task);
      log.info("{}已放入阻塞队列",task.getReceiver().getRemoteAddress());
    }catch (IOException e) {
      e.printStackTrace();
    }
  }
}
origin: songxinjianqwe/Chat

public static <T> byte[] serialize(T obj) {
  Class<T> cls = (Class<T>) obj.getClass();
  LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
  try {
    Schema<T> schema = getSchema(cls);
    return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
  } catch (Exception e) {
    throw new IllegalStateException(e.getMessage(), e);
  } finally {
    buffer.clear();
  }
}

origin: songxinjianqwe/Chat

private void logout() {
  if (!isLogin) {
    return;
  }
  System.out.println("客户端发送下线请求");
  Message message = new Message(
      MessageHeader.builder()
          .type(MessageType.LOGOUT)
          .sender(username)
          .timestamp(System.currentTimeMillis())
          .build(), null);
  try {
    clientChannel.write(ByteBuffer.wrap(ProtoStuffUtil.serialize(message)));
  } catch (IOException e) {
    e.printStackTrace();
  }
}
origin: songxinjianqwe/Chat

byte[] bytes = baos.toByteArray();
baos.close();
Message message = ProtoStuffUtil.deserialize(bytes, Message.class);
MessageHandler messageHandler = SpringContextUtil.getBean("MessageHandler", message.getHeader().getType().toString().toLowerCase());
try {
origin: songxinjianqwe/Chat

  public void handle(SocketChannel channel,Message message) {
    try {
      byte[] response = ProtoStuffUtil.serialize(
          new Response(
              ResponseHeader.builder()
                  .type(ResponseType.PROMPT)
                  .sender(message.getHeader().getSender())
                  .timestamp(message.getHeader().getTimestamp()).build(),
              PromptMsgProperty.SERVER_ERROR.getBytes(PromptMsgProperty.charset)));
      channel.write(ByteBuffer.wrap(response));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
origin: songxinjianqwe/Chat

            .sender(username)
            .timestamp(System.currentTimeMillis())
            .build(), ProtoStuffUtil.serialize(taskDescription));
  } else {
  clientChannel.write(ByteBuffer.wrap(ProtoStuffUtil.serialize(message)));
} catch (IOException e) {
  e.printStackTrace();
origin: songxinjianqwe/Chat

  @Override
  public void uncaughtException(Thread t, Throwable e) {
    try {
      if (e instanceof TaskException) {
        TaskException taskException = (TaskException) e;
        Task task = taskException.getInfo();
        Message message = task.getMessage();
        byte[] response = ProtoStuffUtil.serialize(
            new Response(
                ResponseHeader.builder()
                    .type(ResponseType.PROMPT)
                    .sender(message.getHeader().getSender())
                    .timestamp(message.getHeader().getTimestamp()).build(),
                PromptMsgProperty.TASK_FAILURE.getBytes(PromptMsgProperty.charset)));
        log.info("返回任务执行失败信息");
        task.getReceiver().write(ByteBuffer.wrap(response));
      }
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
}
origin: songxinjianqwe/Chat

if (receiverChannel == null) {
  byte[] response = ProtoStuffUtil.serialize(
      new Response(
          ResponseHeader.builder()
  clientChannel.write(ByteBuffer.wrap(response));
} else {
  byte[] response = ProtoStuffUtil.serialize(
      new Response(
          ResponseHeader.builder()
origin: songxinjianqwe/Chat

  @Override
  public void handle(Message message, Selector server, SelectionKey client, BlockingQueue<Task> queue, AtomicInteger onlineUsers) {
    try {
      byte[] response = ProtoStuffUtil.serialize(
          new Response(
              ResponseHeader.builder()
                  .type(ResponseType.NORMAL)
                  .sender(message.getHeader().getSender())
                  .timestamp(message.getHeader().getTimestamp()).build(),
                  message.getBody()));
      super.broadcast(response,server);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
origin: songxinjianqwe/Chat

SocketChannel clientChannel = (SocketChannel) client.channel();
userManager.logout(clientChannel);
byte[] response = ProtoStuffUtil.serialize(
    new Response(ResponseHeader.builder()
        .type(ResponseType.PROMPT)
onlineUsers.decrementAndGet();
byte[] logoutBroadcast = ProtoStuffUtil.serialize(
    new Response(
        ResponseHeader.builder()
origin: songxinjianqwe/Chat

try {
  if (userManager.login(clientChannel, username, password)) {
    byte[] response = ProtoStuffUtil.serialize(
        new Response(
            ResponseHeader.builder()
    byte[] loginBroadcast = ProtoStuffUtil.serialize(
        new Response(
            ResponseHeader.builder()
    byte[] response = ProtoStuffUtil.serialize(
        new Response(
            ResponseHeader.builder()
cn.sinjinsong.common.util

Most used classes

  • ProtoStuffUtil
    Created by SinjinSong on 2017/5/22.
  • DateTimeUtil
    Created by SinjinSong on 2017/4/23.
  • FileUtil
    Created by SinjinSong on 2017/5/23.
  • ZipUtil
    Created by SinjinSong on 2017/5/25.
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now