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

reactor.tcp.encoding.DelimitedCodec Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011-2013 GoPivotal, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package reactor.tcp.encoding;

import reactor.function.Consumer;
import reactor.function.Function;
import reactor.io.Buffer;
import reactor.io.Buffer.View;

/**
 * An implementation of {@link Codec} that decodes by splitting a {@link Buffer} into segments
 * based on a delimiter and encodes by appending its delimiter to each piece of output.
 * During decoding the delegate is used to process each segment. During encoding the delegate
 * is used to create a buffer for each piece of output to which the delimiter is then appended.
 *
 * @param  The type that will be produced by decoding
 * @param  The type that will be consumed by encoding
 *
 * @author Jon Brisbin
 */
public class DelimitedCodec implements Codec {

	private final Codec delegate;
	private final byte                   delimiter;
	private final boolean                stripDelimiter;

	/**
	 * Create a line-feed-delimited codec, using the given {@code Codec} as a delegate.
	 *
	 * @param delegate The delegate {@link Codec}.
	 */
	public DelimitedCodec(Codec delegate) {
		this((byte) 10, true, delegate);

	}

	/**
	 * Create a line-feed-delimited codec, using the given {@code Codec} as a delegate.
	 *
	 * @param stripDelimiter Flag to indicate whether the delimiter should be stripped from the
	 *                       chunk or not during decoding.
	 * @param delegate       The delegate {@link Codec}.
	 */
	public DelimitedCodec(boolean stripDelimiter, Codec delegate) {
		this((byte) 10, stripDelimiter, delegate);

	}

	/**
	 * Create a delimited codec using the given delimiter and using the given {@code Codec}
	 * as a delegate.
	 *
	 * @param delimiter      The delimiter to use.
	 * @param stripDelimiter Flag to indicate whether the delimiter should be stripped from the
	 *                       chunk or not during decoding.
	 * @param delegate       The delegate {@link Codec}.
	 */
	public DelimitedCodec(byte delimiter, boolean stripDelimiter, Codec delegate) {
		this.delimiter = delimiter;
		this.stripDelimiter = stripDelimiter;
		this.delegate = delegate;
	}

	@Override
	public Function decoder(Consumer next) {
		return new DelimitedDecoder(next);
	}

	@Override
	public Function encoder() {
		return new DelimitedEncoder();
	}

	private class DelimitedDecoder implements Function {
		private final Function decoder;

		DelimitedDecoder(Consumer next) {
			this.decoder = delegate.decoder(next);
		}

		@Override
		public IN apply(Buffer bytes) {
			if (bytes.remaining() == 0) {
				return null;
			}

			Iterable views = bytes.split(delimiter, stripDelimiter);

			int limit = bytes.limit();
			int position = bytes.position();

			for (Buffer.View view : views) {
				Buffer b = view.get();
				decoder.apply(b);
			}

			bytes.limit(limit);
			bytes.position(position);

			return null;
		}
	}

	private class DelimitedEncoder implements Function {
		Function encoder = delegate.encoder();

		@Override
		@SuppressWarnings("resource")
		public Buffer apply(OUT out) {
			Buffer buffer = new Buffer();
			Buffer encoded = encoder.apply(out);
			if (null != encoded && encoded.remaining() > 0) {
				buffer.append(encoded).append(delimiter);
			}
			return buffer.flip();
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy