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

nstream.adapter.common.ingress.ContentAssembler Maven / Gradle / Ivy

There is a newer version: 4.14.22
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.common.ingress;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import swim.structure.Value;

/**
 * A special {@code ValueAssembler} that operates solely on predictably
 * formatted textual inputs.
 */
public abstract class ContentAssembler extends ValueAssembler {

  public ContentAssembler(Value settings) {
    super(settings);
  }

  /**
   * The format of the text data that this {@code ContentAssembler} recognizes.
   *
   * @return  a short identifier of this {@code ContentAssembler}'s content type
   */
  public abstract String contentType();

  /**
   * Transforms a "plain" textual {@code InputStream} into Swim's structured
   * object model.
   *
   * @param decodedStream  a UTF-8 encoded, uncompressed text stream
   *
   * @return  the structural representation of the data in the stream
   *
   * @throws IOException  if there was an issue collecting
   */
  public abstract Value assembleStream(InputStream decodedStream) throws AssemblyException, IOException;

  /**
   * Transforms a "plain" byte array into Swim's structured object model.
   *
   * @param bytes  a UTF-8 encoded, uncompressed byte sequence
   *
   * @return  the structural representation of the data in the byte sequence
   */
  public Value assembleBytes(byte[] bytes) throws AssemblyException {
    return assemble(new String(bytes, StandardCharsets.UTF_8));
  }

  /**
   * Reflection-based creator-type utility function.
   *
   * @param settings  constructor arguments
   *
   * @return  a {@code ContentAssembler} constructed against {@code
   * settings.tail()} and with class {@code settings.head().toValue()}
   */
  @SuppressWarnings("unchecked")
  public static ContentAssembler create(Value settings) {
    return (ContentAssembler) (ValueAssembler.create(settings));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy