title: Split TIFF Files to multiple files
body: <p>I will need to split a tiff file to multiple tiff file using the delimiter II* , so I'm using the below code to convert the tiff file to base64 and using the substring to extract the first image . However I'm getting the error as below. Please advise how to extract only the first image from the tiff file using this delimiter II* (base64 code is SUkq).</p>
  
  <p>I'm able to decode to image without performing the sub string.</p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String   index out of range: -1
  at java.lang.String.substring(String.java:1954)
  at EncodeStringTest.main(EncodeStringTest.java:63)
  </code></pre>
  
  <p>Class File</p>
  
  <pre><code>public class EncodeStringTest {
  
  public static void main(String[] args) {
      File file = new File("D:\\Users\\Vinoth\\workspace\\image.tif");
  
      try {
          /*
           * Reading a Image file from file system
           */
          FileInputStream imageInFile = new FileInputStream(file);
          byte imageData[] = new byte[(int)file.length()];
          imageInFile.read(imageData);
  
          /*
           * Converting Image byte array into Base64 String 
           */
          String imageDataString = encodeImage(imageData);
                      System.out.println(imageDataString);
          String result = imageDataString.substring(imageDataString.indexOf("SUkq") + 1, imageDataString.indexOf("SUkq"));
          /*
           * Converting a Base64 String into Image byte array 
           */
                    System.out.println("Resulted String"+imageDataString);
          byte[] imageByteArray = decodeImage(result);
  
          /*
           * Write a image byte array into file system  
           */
          FileOutputStream imageOutFile = 
                              new FileOutputStream("D:\\Users\\Vinoth\\workspace\\image_2.tif");
          imageOutFile.write(imageByteArray);
  
          imageInFile.close();
          imageOutFile.close();
  
          System.out.println("Image Successfully Manipulated!");
      } catch (FileNotFoundException e) {
          System.out.println("Image not found" + e);
      } catch (IOException ioe) {
          System.out.println("Exception while reading the Image " + ioe);
      }
  
  }
  
  
  public static String encodeImage(byte[] imageByteArray){        
      return Base64.encodeBase64URLSafeString(imageByteArray);        
  }
  
  
  public static byte[] decodeImage(String imageDataString) {      
      return Base64.decodeBase64(imageDataString);
  }
  </code></pre>
  
  <p>}</p>