// Path: src/main/java/org/kopi/ebics/exception/EbicsException.java
// public class EbicsException extends Exception {
// 
//   /**
//    * A means to construct a server error.
//    */
//   public EbicsException() {}
// 
//   /**
//    * A means to construct a server error with an additional message.
//    * @param message the exception message
//    */
//   public EbicsException(String message) {
//     super(message);
//   }
//   /**
//    * A means to construct a server error with no additional message.
//    * @param returnCode the ebics return code.
//    */
//   public EbicsException(ReturnCode returnCode) {
//     this.returnCode = returnCode;
//   }
// 
//   /**
//    * A means to construct a server error with an additional message.
//    * @param returnCode the ebics return code.
//    * @param message the additional message.
//    */
//   public EbicsException(ReturnCode returnCode, String message) {
//     super(message);
//     this.returnCode = returnCode;
//   }
// 
//   /**
//    * Returns the standardized error code.
//    * @return the standardized error code.
//    */
//   public ReturnCode getReturnCode() {
//     return returnCode;
//   }
// 
//   // --------------------------------------------------------------------
//   // DATA MEMBERS
//   // --------------------------------------------------------------------
// 
//   private ReturnCode 		returnCode;
//   private static final long 	serialVersionUID = 2728820344946361669L;
// }
// 
// Path: src/main/java/org/kopi/ebics/interfaces/ContentFactory.java
// public interface ContentFactory extends Serializable {
// 
//   /**
//    * Returns a new data source of the data to be sent.
//    * The instance must ensure that the returned stream will
//    * deliver the identical data during the lifetime of this instance.
//    * Nevertheless how often the method will be called.
//    * @return a new data source of the data to be sent.
//    * @throws IOException
//    */
//   public InputStream getContent() throws IOException;
// }


import org.kopi.ebics.interfaces.ContentFactory;
import org.kopi.ebics.utils.Utils;
import javax.crypto.spec.SecretKeySpec;
import org.kopi.ebics.exception.EbicsException;

/*
 * Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * $Id$
 */

package org.kopi.ebics.io;




/**
 * A mean to split a given input file to
 * 1MB portions. this i useful to handle
 * big file uploading.
 *
 * @author Hachani
 *
 */
public class Splitter {

  /**
   * Constructs a new <code>FileSplitter</code> with a given file.
   * @param input the input byte array
   */
  public Splitter(byte[] input) {
    this.input = input;
  }

  /**
   * Reads the input stream and splits it to segments of 1MB size.
   * 
   * <p>EBICS Specification 2.4.2 - 7 Segmentation of the order data:
   * 
   * <p>The following procedure is to be followed with segmentation:
   * <ol>
   *   <li> The order data is ZIP compressed
   *   <li> The compressed order data is encrypted in accordance with Chapter 6.2
   *   <li> The compressed, encrypted order data is base64-coded.
   *    <li> The result is to be verified with regard to the data volume:
   *      <ol> 
   *        <li> If the resulting data volume is below the threshold of 1 MB = 1,048,576 bytes,
   *             the order data can be sent complete as a data segment within one transmission step
   *        <li> If the resulting data volume exceeds 1,048,576 bytes the data is to be
   *             separated sequentially and in a base64-conformant manner into segments
   *             that each have a maximum of 1,048,576 bytes.
   *     </ol>
   * 
   * @param isCompressionEnabled enable compression?
   * @param keySpec the secret key spec
   * @throws EbicsException
   */
  public final void readInput(boolean isCompressionEnabled, SecretKeySpec keySpec)
    throws EbicsException
  {
    try {
      if (isCompressionEnabled) {
	input = Utils.zip(input);
      }
      content = Utils.encrypt(input, keySpec);
      segmentation();
    } catch (Exception e) {
      throw new EbicsException(e.getMessage());
    }
  }

  /**
   * Slits the input into 1MB portions.
   * 
   * <p> EBICS Specification 2.4.2 - 7 Segmentation of the order data:
   * 
   * <p>In Version H003 of the EBICS standard, order data that requires more than 1 MB of storage
   * space in compressed, encrypted and base64-coded form MUST be segmented before
   * transmission, irrespective of the transfer direction (upload/download).
   * 
   */
  private void segmentation() {


// Path: src/main/java/org/kopi/ebics/exception/EbicsException.java
// public class EbicsException extends Exception {
// 
//   /**
//    * A means to construct a server error.
//    */
//   public EbicsException() {}
// 
//   /**
//    * A means to construct a server error with an additional message.
//    * @param message the exception message
//    */
//   public EbicsException(String message) {
//     super(message);
//   }
//   /**
//    * A means to construct a server error with no additional message.
//    * @param returnCode the ebics return code.
//    */
//   public EbicsException(ReturnCode returnCode) {
//     this.returnCode = returnCode;
//   }
// 
//   /**
//    * A means to construct a server error with an additional message.
//    * @param returnCode the ebics return code.
//    * @param message the additional message.
//    */
//   public EbicsException(ReturnCode returnCode, String message) {
//     super(message);
//     this.returnCode = returnCode;
//   }
// 
//   /**
//    * Returns the standardized error code.
//    * @return the standardized error code.
//    */
//   public ReturnCode getReturnCode() {
//     return returnCode;
//   }
// 
//   // --------------------------------------------------------------------
//   // DATA MEMBERS
//   // --------------------------------------------------------------------
// 
//   private ReturnCode 		returnCode;
//   private static final long 	serialVersionUID = 2728820344946361669L;
// }
// 
// Path: src/main/java/org/kopi/ebics/interfaces/ContentFactory.java
// public interface ContentFactory extends Serializable {
// 
//   /**
//    * Returns a new data source of the data to be sent.
//    * The instance must ensure that the returned stream will
//    * deliver the identical data during the lifetime of this instance.
//    * Nevertheless how often the method will be called.
//    * @return a new data source of the data to be sent.
//    * @throws IOException
//    */
//   public InputStream getContent() throws IOException;
// }


// Path: src/main/java/org/kopi/ebics/io/Splitter.java
import org.kopi.ebics.interfaces.ContentFactory;
import org.kopi.ebics.utils.Utils;
import javax.crypto.spec.SecretKeySpec;
import org.kopi.ebics.exception.EbicsException;

/*
 * Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * $Id$
 */

package org.kopi.ebics.io;




/**
 * A mean to split a given input file to
 * 1MB portions. this i useful to handle
 * big file uploading.
 *
 * @author Hachani
 *
 */
public class Splitter {

  /**
   * Constructs a new <code>FileSplitter</code> with a given file.
   * @param input the input byte array
   */
  public Splitter(byte[] input) {
    this.input = input;
  }

  /**
   * Reads the input stream and splits it to segments of 1MB size.
   * 
   * <p>EBICS Specification 2.4.2 - 7 Segmentation of the order data:
   * 
   * <p>The following procedure is to be followed with segmentation:
   * <ol>
   *   <li> The order data is ZIP compressed
   *   <li> The compressed order data is encrypted in accordance with Chapter 6.2
   *   <li> The compressed, encrypted order data is base64-coded.
   *    <li> The result is to be verified with regard to the data volume:
   *      <ol> 
   *        <li> If the resulting data volume is below the threshold of 1 MB = 1,048,576 bytes,
   *             the order data can be sent complete as a data segment within one transmission step
   *        <li> If the resulting data volume exceeds 1,048,576 bytes the data is to be
   *             separated sequentially and in a base64-conformant manner into segments
   *             that each have a maximum of 1,048,576 bytes.
   *     </ol>
   * 
   * @param isCompressionEnabled enable compression?
   * @param keySpec the secret key spec
   * @throws EbicsException
   */
  public final void readInput(boolean isCompressionEnabled, SecretKeySpec keySpec)
    throws EbicsException
  {
    try {
      if (isCompressionEnabled) {
	input = Utils.zip(input);
      }
      content = Utils.encrypt(input, keySpec);
      segmentation();
    } catch (Exception e) {
      throw new EbicsException(e.getMessage());
    }
  }

  /**
   * Slits the input into 1MB portions.
   * 
   * <p> EBICS Specification 2.4.2 - 7 Segmentation of the order data:
   * 
   * <p>In Version H003 of the EBICS standard, order data that requires more than 1 MB of storage
   * space in compressed, encrypted and base64-coded form MUST be segmented before
   * transmission, irrespective of the transfer direction (upload/download).
   * 
   */
  private void segmentation() {


    numSegments = content.length / 1048576; //(1024 * 1024)
