if (fileStream != null) fileStream.close();
if (inStream != null) { inStream.close(); }
    public byte[] readSegment(int segmentMarker) throws JpegProcessingException    {        InputStream fileStream = null;        int offset = 0;        try {            fileStream = new BufferedInputStream(new FileInputStream(_file));            // first two bytes should be jpeg magic number            if (!isValidJpegHeaderBytes(fileStream)) {                throw new IOException("not a jpeg file");            }            offset += 2;            do {                // next byte is 0xFF                byte segmentIdentifier = (byte)(fileStream.read() & 0xFF);                if ((segmentIdentifier & 0xFF) != 0xFF) {                    throw new JpegProcessingException("expected jpeg segment start identifier 0xFF at offset " + offset + ", not 0x" + Integer.toHexString(segmentIdentifier & 0xFF));                }                offset++;                // next byte is <segment-marker>                byte thisSegmentMarker = (byte)(fileStream.read() & 0xFF);                offset++;                // next 2-bytes are <segment-size>: [high-byte] [low-byte]                byte[] segmentLengthBytes = new byte[2];                fileStream.read(segmentLengthBytes, 0, 2);                offset += 2;                int segmentLength = ((segmentLengthBytes[0] << 8) & 0xFF00) | (segmentLengthBytes[1] & 0xFF);                // segment length includes size bytes, so subtract two                segmentLength -= 2;                if (segmentLength > fileStream.available()) {                    throw new JpegProcessingException("segment size would extend beyond file stream length");                }                byte[] segmentBytes = new byte[segmentLength];                fileStream.read(segmentBytes, 0, segmentLength);                offset += segmentLength;                if ((thisSegmentMarker & 0xFF) == (segmentMarker & 0xFF)) {                    return segmentBytes;                } else if ((thisSegmentMarker & 0xFF) == (SEGMENT_MARKER_SOS & 0xFF)) {                    // The 'Start-Of-Scan' segment's length doesn't include the image data, instead would                    // have to search for the two bytes: 0xFF 0xD9 (EOI).                    // It comes last so simply return at this point                    return null;                } else if ((thisSegmentMarker & 0xFF) == (SEGMENT_MARKER_EOI & 0xFF)) {                    // the 'End-Of-Image' segment -- this should never be found in this fashion                    return null;                }                // didn't find the one we're looking for, loop through to the next segment            } while (true);        } catch (IOException ioe) {            throw new JpegProcessingException("IOException processing Jpeg file", ioe);        } finally {            try {                if (fileStream != null) fileStream.close();            } catch (IOException ioe) {                throw new JpegProcessingException("IOException processing Jpeg file", ioe);            }        }    }