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

org.crsh.command.pipeline.ToChunkPipe Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
/*
 * Copyright (C) 2012 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.crsh.command.pipeline;

import org.crsh.command.CommandContext;
import org.crsh.shell.ScreenContext;
import org.crsh.text.Chunk;
import org.crsh.text.ChunkAdapter;

import java.io.IOException;

/** @author Julien Viet */
public class ToChunkPipe> extends AbstractPipe {

  /** . */
  private ChunkAdapter adapter;

  /** . */
  private Class consumedType;

  public ToChunkPipe(Class consumedType, boolean piped) {
    super(piped);

    //
    this.consumedType = consumedType;
  }

  @Override
  public void open(final CONS consumer) {
    super.open(consumer);

    //
    adapter = new ChunkAdapter(new ScreenContext() {
      public int getWidth() {
        return consumer.getWidth();
      }
      public int getHeight() {
        return consumer.getHeight();
      }
      public Class getConsumedType() {
        return Chunk.class;
      }
      public void provide(Chunk element) throws IOException {
        consumer.provide(element);
      }
      public void flush() throws IOException {
        consumer.flush();
      }
      public void write(Chunk chunk) throws IOException {
        consumer.write(chunk);
      }
    });
  }

  public void provide(C element) throws IOException {
    adapter.provide(element);
  }

  @Override
  public void flush() throws IOException {
    adapter.flush();
  }

  @Override
  public void close() throws IOException {
    adapter.flush();
    super.close();
  }

  public void write(Chunk chunk) throws IOException {
    adapter.write(chunk);
  }

  public Class getProducedType() {
    return Chunk.class;
  }

  public Class getConsumedType() {
    return consumedType;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy