gt:         if (handler == null) 
context: /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package org.dcm4che.tool.storescu;
  
  import java.io.BufferedInputStream; 
  import java.io.EOFException; 
  import java.io.File; 
  import java.io.FileInputStream; 
  import java.io.FileOutputStream; 
  import java.io.FilterInputStream; 
  import java.io.IOException; 
  import java.io.InputStream; 
  import java.io.ObjectInputStream; 
  import java.net.URI; 
  import java.net.URISyntaxException; 
  import java.util.ArrayList; 
  import java.util.Arrays; 
  import java.util.Collections; 
  import java.util.List; 
  import java.util.zip.Inflater; 
  import java.util.zip.InflaterInputStream; 
   
  import org.dcm4che.data.Attributes; 
  import org.dcm4che.data.BulkDataLocator; 
  import org.dcm4che.data.ElementDictionary; 
  import org.dcm4che.data.Fragments; 
  import org.dcm4che.data.ItemPointer; 
  import org.dcm4che.data.Sequence; 
  import org.dcm4che.data.Tag; 
  import org.dcm4che.data.UID; 
  import org.dcm4che.data.VR; 
  import org.dcm4che.io.DicomStreamException;
  import org.dcm4che.util.ByteUtils; 
  import org.dcm4che.util.SafeClose; 
  import org.dcm4che.util.StreamUtils; 
  import org.dcm4che.util.TagUtils; 
  import org.slf4j.Logger; 
  import org.slf4j.LoggerFactory; 
   
  /**
   * @author Gunter Zeilinger <gunterze@gmail.com> 
   */ 
  public class DicomInputStream extends FilterInputStream 
      implements DicomInputHandler { 
   
      private static final Logger LOG =  
          LoggerFactory.getLogger(DicomInputStream.class); 
   
      private static final String UNEXPECTED_NON_ZERO_ITEM_LENGTH = 
          "Unexpected item value of {} #{} @ {}"; 
      private static final String UNEXPECTED_ATTRIBUTE = 
          "Unexpected attribute {} #{} @ {}"; 
      private static final String MISSING_TRANSFER_SYNTAX = 
          "Missing Transfer Syntax (0002,0010) - assume Explicit VR Little Endian"; 
      private static final String MISSING_FMI_LENGTH = 
          "Missing or wrong File Meta Information Group Length (0002,0000)"; 
      private static final String NOT_A_DICOM_STREAM =  
          "Not a DICOM Stream"; 
      private static final String IMPLICIT_VR_BIG_ENDIAN = 
          "Implicit VR Big Endian encoded DICOM Stream"; 
      private static final String DEFLATED_WITH_ZLIB_HEADER = 
          "Deflated DICOM Stream with ZLIB Header"; 
   
      private static final int ZLIB_HEADER = 0x789c; 
      private static final int ALLOC_INC = 0x4000000; // 64 MiB 
   
      private String uri; 
      private String tsuid; 
      private byte[] preamble; 
      private Attributes fileMetaInformation; 
      private boolean hasfmi; 
      private boolean bigEndian; 
      private boolean explicitVR; 
      private boolean includeBulkData = true; 
      private boolean includeBulkDataLocator; 
      private long pos; 
      private long fmiEndPos = -1L; 
      private long tagPos; 
      private long markPos; 
      private int tag; 
      private VR vr; 
      private int length; 
      private DicomInputHandler handler = this; 
      private Attributes bulkData; 
      private final byte[] buffer = new byte[12]; 
      private ItemPointer[] itemPointers = {}; 
      private int level; 
   
      private boolean catBlkFiles; 
      private String blkFilePrefix = "blk"; 
      private String blkFileSuffix; 
      private File blkDirectory; 
      private ArrayList<File> blkFiles; 
      private String blkURI; 
      private FileOutputStream blkOut; 
      private long blkOutPos; 
   
      public static Attributes defaultBulkData() { 
          Attributes bulkData = new Attributes(7); 
          bulkData.setNull(Tag.PixelDataProviderURL, VR.UT); 
          bulkData.setNull(Tag.AudioSampleData, VR.OB); 
          bulkData.setNull(Tag.CurveData, VR.OB); 
          Attributes wfsqitem = new Attributes(1); 
          bulkData.newSequence(Tag.WaveformSequence, 1).add(wfsqitem); 
          bulkData.setNull(Tag.SpectroscopyData, VR.OF); 
          wfsqitem.setNull(Tag.WaveformData, VR.OB); 
          bulkData.setNull(Tag.OverlayData, VR.OB); 
          bulkData.setNull(Tag.PixelData, VR.OB); 
          return bulkData; 
      } 
   
      public DicomInputStream(InputStream in, String tsuid) throws IOException { 
          super(in); 
          switchTransferSyntax(tsuid); 
      } 
   
      public DicomInputStream(InputStream in) throws IOException { 
          super(in.markSupported() ? in : new BufferedInputStream(in)); 
          guessTransferSyntax(); 
      } 
   
      public DicomInputStream(File file) throws IOException { 
          this(new FileInputStream(file)); 
          uri = file.toURI().toString(); 
      } 
   
      public final String getURI() { 
          return uri; 
      } 
   
      public final void setURI(String uri) { 
          if (uri != null) 
              try { 
                  new URI(uri); 
              } catch (URISyntaxException e) { 
                  throw new IllegalArgumentException(e.getMessage()); 
              } 
          this.uri = uri; 
      } 
   
      public final boolean isIncludeBulkData() { 
          return includeBulkData; 
      } 
   
      public final void setIncludeBulkData(boolean includeBulkData) { 
          this.includeBulkData = includeBulkData; 
          if (includeBulkData) 
              includeBulkDataLocator = false; 
      } 
   
      public final boolean isIncludeBulkDataLocator() { 
          return includeBulkDataLocator; 
      } 
   
      public final void setIncludeBulkDataLocator(boolean includeBulkDataLocator) { 
          this.includeBulkDataLocator = includeBulkDataLocator; 
          if (includeBulkDataLocator) 
              includeBulkData = false; 
      } 
   
      public final String getBulkDataFilePrefix() { 
          return blkFilePrefix; 
      } 
   
      public final void setBulkDataFilePrefix(String blkFilePrefix) { 
          this.blkFilePrefix = blkFilePrefix; 
      } 
   
      public final String getBulkDataFileSuffix() { 
          return blkFileSuffix; 
      } 
   
      public final void setBulkDataFileSuffix(String blkFileSuffix) { 
          this.blkFileSuffix = blkFileSuffix; 
      } 
   
      public final File getBulkDataDirectory() { 
          return blkDirectory; 
      } 
   
      public final void setBulkDataDirectory(File blkDirectory) { 
          this.blkDirectory = blkDirectory; 
      } 
   
      public final boolean isConcatenateBulkDataFiles() { 
          return catBlkFiles; 
      } 
   
      public final void setConcatenateBulkDataFiles(boolean catBlkFiles) { 
          this.catBlkFiles = catBlkFiles; 
      } 
   
      public final List<File> getBulkDataFiles() { 
          if (blkFiles != null) 
              return blkFiles; 
          else 
              return Collections.emptyList(); 
      } 
   
      public final Attributes getBulkDataAttributes() { 
          return bulkData; 
      } 
   
      public final void setBulkDataAttributes(Attributes bulkData) { 
          this.bulkData = bulkData; 
      } 
   
      public final void setDicomInputHandler(DicomInputHandler handler) { 
  