All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.eventbus.impl.StringMessage Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.jsync.eventbus.impl;

import io.jsync.buffer.Buffer;
import io.jsync.eventbus.Message;
import io.netty.util.CharsetUtil;

/**
 * @author Tim Fox
 */
class StringMessage extends BaseMessage {

  private byte[] encoded;

  StringMessage(boolean send, String address, String body) {
    super(send, address, body);
  }

  public StringMessage(Buffer readBuff) {
    super(readBuff);
  }

  @Override
  protected void readBody(int pos, Buffer readBuff) {
    boolean isNull = readBuff.getByte(pos) == (byte)0;
    if (!isNull) {
      pos++;
      int strLength = readBuff.getInt(pos);
      pos += 4;
      byte[] bytes = readBuff.getBytes(pos, pos + strLength);
      body = new String(bytes, CharsetUtil.UTF_8);
    }
  }

  @Override
  protected void writeBody(Buffer buff) {
    if (body == null) {
      buff.appendByte((byte)0);
    } else {
      buff.appendByte((byte)1);
      buff.appendInt(encoded.length);
      buff.appendBytes(encoded);
    }
    encoded = null;
  }

  @Override
  protected int getBodyLength() {
    if (body == null) {
      return 1;
    } else {
      encoded = body.getBytes(CharsetUtil.UTF_8);
      return 1 + 4 + encoded.length;
    }
  }

  @Override
  protected Message copy() {
    // No need to copy since everything is immutable
    return this;
  }

  @Override
  protected byte type() {
    return MessageFactory.TYPE_STRING;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy