/**
   * Method to verify if receieved stream has content or its empty
   *
   * @param stream Stream to check
   * @return verified stream
   * @throws IOException
   */
  private InputStream verifyContentReceieved(InputStream stream) throws IOException {

    if (stream == null) { // Check if its null
      logger.debug("Request stream received as null");
      return null;
    } else if (stream.markSupported()) { // Check stream supports mark/reset
      // mark() and read the first byte just to check
      stream.mark(1);
      final int bytesRead = stream.read(new byte[1]);
      if (bytesRead != -1) {
        // stream not empty
        stream.reset(); // reset the stream as if untouched
        return stream;
      } else {
        // stream empty
        logger.debug("Request received with empty body");
        return null;
      }
    } else {
      // Panic! this stream does not support mark/reset, try with PushbackInputStream as a last
      // resort
      int bytesRead;
      PushbackInputStream pbs = new PushbackInputStream(stream);
      if ((bytesRead = pbs.read()) != -1) {
        // Contents detected, unread and return
        pbs.unread(bytesRead);
        return pbs;
      } else {
        // Empty stream detected
        logger.debug("Request received with empty body!");
        return null;
      }
    }
  }
  public Object nextChunk() throws Exception {
    if (!hasNextChunk()) {
      return null;
    }

    final int availableBytes = in.available();
    final int chunkSize;
    if (availableBytes <= 0) {
      chunkSize = this.chunkSize;
    } else {
      chunkSize = Math.min(this.chunkSize, in.available());
    }
    final byte[] chunk = new byte[chunkSize];
    int readBytes = 0;
    for (; ; ) {
      int localReadBytes = in.read(chunk, readBytes, chunkSize - readBytes);
      if (localReadBytes < 0) {
        break;
      }
      readBytes += localReadBytes;
      offset += localReadBytes;

      if (readBytes == chunkSize) {
        break;
      }
    }

    return wrappedBuffer(chunk, 0, readBytes);
  }
 private DataBag consumeBag(PushbackInputStream in, ResourceFieldSchema fieldSchema)
     throws IOException {
   if (fieldSchema == null) {
     throw new IOException("Schema is null");
   }
   ResourceFieldSchema[] fss = fieldSchema.getSchema().getFields();
   Tuple t;
   int buf;
   while ((buf = in.read()) != '{') {
     if (buf == -1) {
       throw new IOException("Unexpect end of bag");
     }
   }
   if (fss.length != 1) throw new IOException("Only tuple is allowed inside bag schema");
   ResourceFieldSchema fs = fss[0];
   DataBag db = DefaultBagFactory.getInstance().newDefaultBag();
   while (true) {
     t = consumeTuple(in, fs);
     if (t != null) db.add(t);
     while ((buf = in.read()) != '}' && buf != ',') {
       if (buf == -1) {
         throw new IOException("Unexpect end of bag");
       }
     }
     if (buf == '}') break;
   }
   return db;
 }
  /**
   * This method allows the caller to retrieve any data that might have been sent despite the fact
   * that an error occurred. For example, the HTML page sent along with a 404 File Not Found error.
   * If the socket is not connected, or if no error occurred or no data was returned, this method
   * returns <code>null</code>.
   *
   * @return An <code>InputStream</code> for reading error data.
   */
  public InputStream getErrorStream() {
    if (!connected) return (null);

    int code;
    try {
      code = getResponseCode();
    } catch (IOException e) {
      code = -1;
    }

    if (code == -1) return (null);

    if (((code / 100) != 4) || ((code / 100) != 5)) return (null);

    try {
      PushbackInputStream pbis = new PushbackInputStream(getInputStream());

      int i = pbis.read();
      if (i == -1) return (null);

      pbis.unread(i);
      return (pbis);
    } catch (IOException e) {
      return (null);
    }
  }
Exemple #5
0
  public static String unGzipBytesToString(InputStream in) {

    try {
      PushbackInputStream pis = new PushbackInputStream(in, 2);
      byte[] signature = new byte[2];
      pis.read(signature);
      pis.unread(signature);
      int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00));
      if (head != GZIPInputStream.GZIP_MAGIC) {
        return new String(toByteArray(pis), "UTF-8").trim();
      }
      GZIPInputStream gzip = new GZIPInputStream(pis);
      byte[] readBuf = new byte[8 * 1024];
      ByteArrayOutputStream outputByte = new ByteArrayOutputStream();
      int readCount = 0;
      do {
        readCount = gzip.read(readBuf);
        if (readCount > 0) {
          outputByte.write(readBuf, 0, readCount);
        }
      } while (readCount > 0);
      closeQuietly(gzip);
      closeQuietly(pis);
      closeQuietly(in);
      if (outputByte.size() > 0) {
        return new String(outputByte.toByteArray(), "UTF-8");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  /**
   * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only
   * BOM bytes are skipped.
   */
  protected void init() throws IOException {
    if (internalIn2 != null) return;

    Charset encoding;
    byte bom[] = new byte[BOM_SIZE];
    int n, unread;
    n = internalIn.read(bom, 0, bom.length);

    if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
      encoding = UTF8;
      unread = n - 3;
    } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
      encoding = UTF16BE;
      unread = n - 2;
    } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
      encoding = UTF16LE;
      unread = n - 2;
    } else {
      // Unicode BOM mark not found, unread all bytes
      encoding = UTF8;
      unread = n;
    }

    if (unread > 0) internalIn.unread(bom, (n - unread), unread);

    // Use given encoding
    internalIn2 = new InputStreamReader(internalIn, encoding);
  }
  private Map<String, Object> consumeMap(PushbackInputStream in, ResourceFieldSchema fieldSchema)
      throws IOException {
    int buf;

    while ((buf = in.read()) != '[') {
      if (buf == -1) {
        throw new IOException("Unexpect end of map");
      }
    }
    HashMap<String, Object> m = new HashMap<String, Object>();
    ByteArrayOutputStream mOut = new ByteArrayOutputStream(BUFFER_SIZE);
    while (true) {
      // Read key (assume key can not contains special character such as #, (, [, {, }, ], )
      while ((buf = in.read()) != '#') {
        if (buf == -1) {
          throw new IOException("Unexpect end of map");
        }
        mOut.write(buf);
      }
      String key = bytesToCharArray(mOut.toByteArray());
      if (key.length() == 0) throw new IOException("Map key can not be null");

      // Read value
      mOut.reset();
      Deque<Character> level =
          new LinkedList<
              Character>(); // keep track of nested tuple/bag/map. We do not interpret, save them as
      // bytearray
      while (true) {
        buf = in.read();
        if (buf == -1) {
          throw new IOException("Unexpect end of map");
        }
        if (buf == '[' || buf == '{' || buf == '(') {
          level.push((char) buf);
        } else if (buf == ']' && level.isEmpty()) // End of map
        break;
        else if (buf == ']' || buf == '}' || buf == ')') {
          if (level.isEmpty()) throw new IOException("Malformed map");

          if (level.peek() == findStartChar((char) buf)) level.pop();
        } else if (buf == ',' && level.isEmpty()) { // Current map item complete
          break;
        }
        mOut.write(buf);
      }
      Object value = null;
      if (fieldSchema != null && fieldSchema.getSchema() != null && mOut.size() > 0) {
        value = bytesToObject(mOut.toByteArray(), fieldSchema.getSchema().getFields()[0]);
      } else if (mOut.size() > 0) { // untyped map
        value = new DataByteArray(mOut.toByteArray());
      }
      m.put(key, value);
      mOut.reset();
      if (buf == ']') break;
    }
    return m;
  }
  public static ImageType guessImageType(PushbackInputStream is) throws IOException {
    // Read the first ImageIO.IMAGE_MAGIC_NUMBER_LEN bytes
    byte[] magicNumber = new byte[IMAGE_MAGIC_NUMBER_LEN];
    is.read(magicNumber);
    ImageType imageType = guessImageType(magicNumber);
    is.unread(magicNumber); // reset stream pointer

    return imageType;
  }
 public boolean hasNextChunk() throws Exception {
   int b = in.read();
   if (b < 0) {
     return false;
   } else {
     in.unread(b);
     return true;
   }
 }
Exemple #10
0
 private int getChar() throws IOException {
   int c = is.read();
   if (c == '\r') {
     int next = is.read();
     if (next != '\n') is.unread(next);
     c = '\n';
   }
   if (c == '\n') line++;
   return c;
 }
Exemple #11
0
  public Object read(InputStream is) throws IOException {
    PushbackInputStream pbis = (PushbackInputStream) pbMap.get(is);
    if (null == pbis) {
      pbis = new PushbackInputStream(is, PUSHBACK_BUFFER_SIZE);
      PushbackInputStream prev = (PushbackInputStream) pbMap.putIfAbsent(is, pbis);
      pbis = null == prev ? pbis : prev;
    }

    int len = -1;
    try {
      // read until xml pattern is seen (and then pushed back) or no more data
      // to read. return all data as message
      byte[] buffer = new byte[READ_BUFFER_SIZE];
      StringBuffer message = new StringBuffer(READ_BUFFER_SIZE);
      int patternIndex = -1;
      boolean repeat;
      do {
        len = safeRead(pbis, buffer);
        if (len >= 0) {
          // TODO take encoding into account, ideally from the incoming XML
          message.append(new String(buffer, 0, len));
          // start search at 2nd character in buffer (index=1) to
          // indicate whether we have reached a new document.
          patternIndex = message.toString().indexOf(XML_PATTERN, 1);
          repeat = isRepeat(patternIndex, len, pbis.available());
        } else {
          // never repeat on closed stream (and avoid calling available)
          repeat = false;
        }

      } while (repeat);

      if (patternIndex > 0) {
        // push back the start of the next message and
        // ignore the pushed-back characters in the return buffer
        pbis.unread(message.substring(patternIndex, message.length()).getBytes());
        message.setLength(patternIndex);
      }

      // TODO encoding here, too...
      return nullEmptyArray(message.toString().getBytes());

    } finally {
      // TODO - this doesn't seem very reliable, since loop above can end
      // without EOF.  On the other hand, what else can we do?  Entire logic
      // is not very dependable, IMHO.  XmlMessageEOFProtocol is more likely
      // to be correct here, I think.

      // clear from map if stream has ended
      if (len < 0) {
        pbMap.remove(is);
      }
    }
  }
Exemple #12
0
 private int normalizeRead(PushbackInputStream str) throws IOException {
   int ret = str.read();
   if (ret == '\r') {
     int tmp = str.read();
     if (tmp != '\n') {
       str.unread(tmp);
     }
     ret = '\n';
   }
   return ret;
 }
Exemple #13
0
 public static InputStream decompressStream(InputStream input) throws IOException {
   PushbackInputStream pb =
       new PushbackInputStream(input, 2); // we need a pushbackstream to look ahead
   byte[] signature = new byte[2];
   pb.read(signature); // read the signature
   pb.unread(signature); // push back the signature to the stream
   if (signature[0] == (byte) 0x1f
       && signature[1] == (byte) 0x8b) // check if matches standard gzip magic number
   return new GZIPInputStream(pb);
   else return pb;
 }
  private static String getContentEncoding(PushbackInputStream is) throws IOException {
    byte[] signature = new byte[2];

    int count = is.read(signature);

    is.unread(signature, 0, count);

    if ((count == signature.length) && Arrays.equals(Deployer.GZIP_SIGNATURE, signature)) {
      return "gzip";
    }

    return null;
  }
Exemple #15
0
 /** 判断并移除UTF-8的BOM头 */
 public static InputStream utf8filte(InputStream in) {
   try {
     if (in.available() == -1) return in;
     PushbackInputStream pis = new PushbackInputStream(in, 3);
     byte[] header = new byte[3];
     int len = pis.read(header, 0, 3);
     if (len < 1) return in;
     if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) {
       pis.unread(header, 0, len);
     }
     return pis;
   } catch (IOException e) {
     throw Lang.wrapThrow(e);
   }
 }
 /**
  * Asserts that advancePastNextSyncMarker advances an input stream past a sync marker and
  * correctly returns the number of bytes consumed from the stream. Creates a haystack of size
  * bytes and places a 16-byte sync marker at the position specified.
  */
 private void testAdvancePastNextSyncMarkerAt(int position, int size) throws IOException {
   byte sentinel = (byte) 0xFF;
   byte[] marker = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
   byte[] haystack = createHaystack(marker, position, size);
   PushbackInputStream stream =
       new PushbackInputStream(new ByteArrayInputStream(haystack), marker.length);
   if (position + marker.length < size) {
     haystack[position + marker.length] = sentinel;
     assertEquals(position + marker.length, AvroReader.advancePastNextSyncMarker(stream, marker));
     assertEquals(sentinel, (byte) stream.read());
   } else {
     assertEquals(size, AvroReader.advancePastNextSyncMarker(stream, marker));
     assertEquals(-1, stream.read());
   }
 }
  /** Determines if the next 4 bytes of the stream represent a frame header. */
  public boolean isSyncCurrentPosition(int syncmode) throws BitstreamException {
    int read = readBytes(syncbuf, 0, 4);
    int headerstring =
        ((syncbuf[0] << 24) & 0xFF000000)
            | ((syncbuf[1] << 16) & 0x00FF0000)
            | ((syncbuf[2] << 8) & 0x0000FF00)
            | ((syncbuf[3] << 0) & 0x000000FF);

    try {
      source.unread(syncbuf, 0, read);
    } catch (IOException ex) {
    }

    boolean sync = false;
    switch (read) {
      case 0:
        sync = true;
        break;
      case 4:
        sync = isSyncMark(headerstring, syncmode, syncword);
        break;
    }

    return sync;
  }
 /**
  * Close the Bitstream.
  *
  * @throws BitstreamException
  */
 public void close() throws BitstreamException {
   try {
     source.close();
   } catch (IOException ex) {
     throw newBitstreamException(STREAM_ERROR, ex);
   }
 }
Exemple #19
0
  private InputStream decompress(InputStream in) throws IOException {

    PushbackInputStream pushbackIn = new PushbackInputStream(in, 2);
    int b1 = pushbackIn.read();
    int b2 = pushbackIn.read();
    pushbackIn.unread(b2);
    pushbackIn.unread(b1);

    if (b1 == GzFileConnection.GZIP_MAGIC_BYTE1 && b2 == GzFileConnection.GZIP_MAGIC_BYTE2) {
      return new GZIPInputStream(pushbackIn);

    } else if (b1 == 0xFD && b2 == '7') {
      return new XZInputStream(pushbackIn);
    }
    return in;
  }
Exemple #20
0
 /** Closes any files opened by this tokenizer. */
 public void close() {
   if (wantClose) {
     try {
       is.close();
     } catch (IOException e) {
     }
   }
 }
  /**
   * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only
   * BOM bytes are skipped.
   */
  protected void init() throws IOException {
    if (internalIn2 != null) return;

    String encoding;
    byte bom[] = new byte[BOM_SIZE];
    int n, unread;
    n = internalIn.read(bom, 0, bom.length);

    if ((bom[0] == (byte) 0x00)
        && (bom[1] == (byte) 0x00)
        && (bom[2] == (byte) 0xFE)
        && (bom[3] == (byte) 0xFF)) {
      encoding = "UTF-32BE";
      unread = n - 4;
    } else if ((bom[0] == (byte) 0xFF)
        && (bom[1] == (byte) 0xFE)
        && (bom[2] == (byte) 0x00)
        && (bom[3] == (byte) 0x00)) {
      encoding = "UTF-32LE";
      unread = n - 4;
    } else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
      encoding = "UTF-8";
      unread = n - 3;
    } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
      encoding = "UTF-16BE";
      unread = n - 2;
    } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
      encoding = "UTF-16LE";
      unread = n - 2;
    } else {
      // Unicode BOM mark not found, unread all bytes
      encoding = defaultEnc;
      unread = n;
    }
    // System.out.println("read=" + n + ", unread=" + unread);

    if (unread > 0) internalIn.unread(bom, (n - unread), unread);

    // Use given encoding
    if (encoding == null) {
      internalIn2 = new InputStreamReader(internalIn);
    } else {
      internalIn2 = new InputStreamReader(internalIn, encoding);
    }
  }
Exemple #22
0
  @Override
  public void poll() {
    try {
      if (sendStream) {
        PushbackInputStream in = new PushbackInputStream(inputStream);

        // Block until we have some data
        int i = in.read();
        // Roll back our read
        in.unread(i);
        MuleMessage message =
            new DefaultMuleMessage(connector.getMessageAdapter(in), connector.getMuleContext());
        routeMessage(message, endpoint.isSynchronous());
      } else {
        byte[] inputBuffer = new byte[bufferSize];
        int len = inputStream.read(inputBuffer);

        if (len == -1) {
          return;
        }

        StringBuffer fullBuffer = new StringBuffer(bufferSize);
        while (len > 0) {
          fullBuffer.append(new String(inputBuffer, 0, len));
          len = 0; // mark as read
          if (inputStream.available() > 0) {
            len = inputStream.read(inputBuffer);
          }
        }

        // Each line is a separate message
        String[] lines = fullBuffer.toString().split(SystemUtils.LINE_SEPARATOR);
        for (int i = 0; i < lines.length; ++i) {
          routeMessage(
              new DefaultMuleMessage(
                  connector.getMessageAdapter(lines[i]), connector.getMuleContext()),
              endpoint.isSynchronous());
        }
      }

      doConnect();
    } catch (Exception e) {
      handleException(e);
    }
  }
  /**
   * Reads from the compressed stream and stores the resulting uncompressed data into the byte
   * array.
   *
   * @return number of bytes read, or -1 upon EOF
   */
  public int read(byte[] b, int off, int len) throws IOException {
    if (len <= 0 || off < 0 || off + len > b.length) return 0;

    if (_eof) return -1;

    // Read from uncompressed stream
    if (!_isGzip) return _in.read(b, off, len);

    try {
      int sublen;
      int length = 0;
      while (length < len) {
        if (_inflater.needsInput()) {
          _readBufferSize = _in.read(_readBuffer, 0, _readBuffer.length);
          if (_readBufferSize < 0) break;

          _inflater.setInput(_readBuffer, 0, _readBufferSize);
        }

        sublen = _inflater.inflate(b, off + length, len - length);

        _crc.update(b, off + length, sublen);
        _inputSize += sublen;
        _totalInputSize += sublen;

        length += sublen;

        // Unread gzip trailer and possibly beginning of appended gzip data.
        if (_inflater.finished()) {
          int remaining = _inflater.getRemaining();
          _in.unread(_readBuffer, _readBufferSize - remaining, remaining);

          readTrailer();

          int secondPart = read(b, off + length, len - length);

          return secondPart > 0 ? length + secondPart : length;
        }
      }

      return length;
    } catch (DataFormatException e) {
      throw new IOException(e.getMessage());
    }
  }
Exemple #24
0
 private void ungetChar(int c) throws IOException {
   if (c == -1) {
     return;
   }
   is.unread(c);
   if (c == '\n') {
     line--;
   }
 }
Exemple #25
0
  /** Decode one BASE64 atom into 1, 2, or 3 bytes of data. */
  @Override
  protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)
      throws java.io.IOException {
    int i;
    byte a = -1, b = -1, c = -1, d = -1;

    if (rem < 2) {
      throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
    }
    do {
      i = inStream.read();
      if (i == -1) {
        throw new CEStreamExhausted();
      }
    } while (i == '\n' || i == '\r');
    decode_buffer[0] = (byte) i;

    i = readFully(inStream, decode_buffer, 1, rem - 1);
    if (i == -1) {
      throw new CEStreamExhausted();
    }

    if (rem > 3 && decode_buffer[3] == '=') {
      rem = 3;
    }
    if (rem > 2 && decode_buffer[2] == '=') {
      rem = 2;
    }
    switch (rem) {
      case 4:
        d = pem_convert_array[decode_buffer[3] & 0xff];
        // NOBREAK
      case 3:
        c = pem_convert_array[decode_buffer[2] & 0xff];
        // NOBREAK
      case 2:
        b = pem_convert_array[decode_buffer[1] & 0xff];
        a = pem_convert_array[decode_buffer[0] & 0xff];
        break;
    }

    switch (rem) {
      case 2:
        outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
        break;
      case 3:
        outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
        outStream.write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
        break;
      case 4:
        outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
        outStream.write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
        outStream.write((byte) (((c << 6) & 0xc0) | (d & 0x3f)));
        break;
    }
    return;
  }
 // REVIEW: add new error codes for this.
 public void unreadFrame() throws BitstreamException {
   if (wordpointer == -1 && bitindex == -1 && (framesize > 0)) {
     try {
       source.unread(frame_bytes, 0, framesize);
     } catch (IOException ex) {
       throw newBitstreamException(STREAM_ERROR);
     }
   }
 }
Exemple #27
0
  /**
   * Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only
   * BOM bytes are skipped.
   */
  private void init(InputStream in, String defaultEnc) throws IOException {
    String encoding;
    byte bom[] = new byte[BOM_MAX_SIZE];
    int n, unread;
    PushbackInputStream internalIn = new PushbackInputStream(in, BOM_MAX_SIZE);
    n = internalIn.read(bom, 0, bom.length);

    if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
      encoding = "UTF-8";
      unread = n - 3;
    } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
      encoding = "UTF-16BE";
      unread = n - 2;
    } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
      encoding = "UTF-16LE";
      unread = n - 2;
    } else if ((bom[0] == (byte) 0x00)
        && (bom[1] == (byte) 0x00)
        && (bom[2] == (byte) 0xFE)
        && (bom[3] == (byte) 0xFF)) {
      encoding = "UTF-32BE";
      unread = n - 4;
    } else if ((bom[0] == (byte) 0xFF)
        && (bom[1] == (byte) 0xFE)
        && (bom[2] == (byte) 0x00)
        && (bom[3] == (byte) 0x00)) {
      encoding = "UTF-32LE";
      unread = n - 4;
    } else {
      // Unicode BOM mark not found, unread all bytes
      encoding = defaultEnc;
      unread = n;
    }

    if (unread > 0) internalIn.unread(bom, (n - unread), unread);
    else if (unread < -1) internalIn.unread(bom, 0, 0);

    // Use BOM or default encoding
    if (encoding == null) {
      delegate = new InputStreamReader(internalIn);
    } else {
      delegate = new InputStreamReader(internalIn, encoding);
    }
  }
  /**
   * Generates a certificate revocation list (CRL) object and initializes it with the data read from
   * the input stream inStream.
   */
  public CRL engineGenerateCRL(InputStream inStream) throws CRLException {
    if (currentCrlStream == null) {
      currentCrlStream = inStream;
      sCrlData = null;
      sCrlDataObjectCount = 0;
    } else if (currentCrlStream != inStream) // reset if input stream has changed
    {
      currentCrlStream = inStream;
      sCrlData = null;
      sCrlDataObjectCount = 0;
    }

    try {
      if (sCrlData != null) {
        if (sCrlDataObjectCount != sCrlData.size()) {
          return getCRL();
        } else {
          sCrlData = null;
          sCrlDataObjectCount = 0;
          return null;
        }
      }

      PushbackInputStream pis = new PushbackInputStream(inStream);
      int tag = pis.read();

      if (tag == -1) {
        return null;
      }

      pis.unread(tag);

      if (tag != 0x30) // assume ascii PEM encoded.
      {
        return readPEMCRL(pis);
      } else { // lazy evaluate to help processing of large CRLs
        return readDERCRL(new ASN1InputStream(pis, true));
      }
    } catch (CRLException e) {
      throw e;
    } catch (Exception e) {
      throw new CRLException(e.toString());
    }
  }
  /**
   * Checks that the supplied InputStream (which MUST support mark and reset, or be a
   * PushbackInputStream) has a POIFS (OLE2) header at the start of it. If your InputStream does not
   * support mark / reset, then wrap it in a PushBackInputStream, then be sure to always use that,
   * and not the original!
   *
   * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
   */
  public static boolean hasPOIFSHeader(InputStream inp) throws IOException {
    // We want to peek at the first 8 bytes
    inp.mark(8);

    byte[] header = new byte[8];
    IOUtils.readFully(inp, header);
    LongField signature = new LongField(HeaderBlockConstants._signature_offset, header);

    // Wind back those 8 bytes
    if (inp instanceof PushbackInputStream) {
      PushbackInputStream pin = (PushbackInputStream) inp;
      pin.unread(header);
    } else {
      inp.reset();
    }

    // Did it match the signature?
    return (signature.get() == HeaderBlockConstants._signature);
  }
  private ProxyMessage readMsg(InputStream in) throws IOException {
    PushbackInputStream push_in;
    if (in instanceof PushbackInputStream) push_in = (PushbackInputStream) in;
    else push_in = new PushbackInputStream(in);

    int version = push_in.read();
    push_in.unread(version);

    ProxyMessage msg;

    if (version == 5) {
      msg = new Socks5Message(push_in, false);
    } else if (version == 4) {
      msg = new Socks4Message(push_in, false);
    } else {
      throw new SocksException(CProxy.SOCKS_FAILURE);
    }
    return msg;
  }