java.io.PushbackInputStream Java Examples

The following examples show how to use java.io.PushbackInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CachedBlobStore.java    From james-project with Apache License 2.0 6 votes vote down vote up
static RequireStream eager() {
    return in -> length -> {
        //+1 is to evaluate hasMore
        var stream = new PushbackInputStream(in, length + 1);
        var bytes = new byte[length];
        int readByteCount = IOUtils.read(stream, bytes);
        Optional<byte[]> firstBytes;
        boolean hasMore;
        if (readByteCount < 0) {
            firstBytes = Optional.empty();
            hasMore = false;
        } else {
            byte[] readBytes = Arrays.copyOf(bytes, readByteCount);
            hasMore = hasMore(stream);
            stream.unread(readBytes);
            firstBytes = Optional.of(readBytes);
        }
        return new ReadAheadInputStream(stream, firstBytes, hasMore);
    };
}
 
Example #2
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream == null) {
		this.body = null;
	}
	else if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
	this.method = ((HttpRequest) inputMessage).getMethod();
}
 
Example #3
Source File: DefaultAttributes.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Tests whether InputStream contains serialized data
* @param pbStream is pushback input stream; tests 4 bytes and then returns them back
* @return true if the file has serialized form
*/
static private final boolean isSerialized(PushbackInputStream pbStream)
throws IOException {
    int[] serialPattern = { '\u00AC', '\u00ED', '\u0000', '\u0005' }; //NOI18N patern for serialized objects
    byte[] checkedArray = new byte[serialPattern.length];
    int unsignedConv = 0;

    pbStream.read(checkedArray, 0, checkedArray.length);
    pbStream.unread(checkedArray);

    for (int i = 0; i < checkedArray.length; i++) {
        unsignedConv = (checkedArray[i] < 0) ? (checkedArray[i] + 256) : checkedArray[i];

        if (serialPattern[i] != unsignedConv) {
            return false;
        }
    }

    return true;
}
 
Example #4
Source File: UUDecoder.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decode a UU atom. Note that if l is less than 3 we don't write
 * the extra bits, however the encoder always encodes 4 character
 * groups even when they are not needed.
 */
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l)
    throws IOException {
    int i, c1, c2, c3, c4;
    int a, b, c;
    StringBuffer x = new StringBuffer();

    for (i = 0; i < 4; i++) {
        c1 = inStream.read();
        if (c1 == -1) {
            throw new CEStreamExhausted();
        }
        x.append((char)c1);
        decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f);
    }
    a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3);
    b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf);
    c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f);
    outStream.write((byte)(a & 0xff));
    if (l > 1) {
        outStream.write((byte)( b & 0xff));
    }
    if (l > 2) {
        outStream.write((byte)(c&0xff));
    }
}
 
Example #5
Source File: ServerAuthenticatorNone.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 Grants access to everyone.Removes authentication related bytes from
 the stream, when a SOCKS5 connection is being made, selects an
 authentication NONE.
 */
public ServerAuthenticator startSession(Socket s)
                               throws IOException{

  PushbackInputStream in =  new PushbackInputStream(s.getInputStream());
  OutputStream out = s.getOutputStream();

  int version = in.read();
  if(version == 5){
    if(!selectSocks5Authentication(in,out,0))
       return null;
  }else if(version == 4){
    //Else it is the request message allready, version 4
    in.unread(version);
  }else
    return null;
  

  return new ServerAuthenticatorNone(in,out);
}
 
Example #6
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
}
 
Example #7
Source File: HttpResponse.java    From nutch-htmlunit with Apache License 2.0 6 votes vote down vote up
private int parseStatusLine(PushbackInputStream in, StringBuffer line)
  throws IOException, HttpException {
  readLine(in, line, false);

  int codeStart = line.indexOf(" ");
  int codeEnd = line.indexOf(" ", codeStart+1);

  // handle lines with no plaintext result code, ie:
  // "HTTP/1.1 200" vs "HTTP/1.1 200 OK"
  if (codeEnd == -1) 
    codeEnd= line.length();

  int code;
  try {
    code= Integer.parseInt(line.substring(codeStart+1, codeEnd));
  } catch (NumberFormatException e) {
    throw new HttpException("bad status line '" + line 
                            + "': " + e.getMessage(), e);
  }

  return code;
}
 
Example #8
Source File: Utf8StorageConverter.java    From spork with Apache License 2.0 6 votes vote down vote up
private Object consumeComplexType(PushbackInputStream in, ResourceFieldSchema complexFieldSchema) throws IOException {
    Object field;
    switch (complexFieldSchema.getType()) {
    case DataType.BAG:
        field = consumeBag(in, complexFieldSchema);
        break;
    case DataType.TUPLE:
        field = consumeTuple(in, complexFieldSchema);
        break;
    case DataType.MAP:
        field = consumeMap(in, complexFieldSchema);
        break;
    default:
        throw new IOException("Unknown complex data type");
    }
    return field;
}
 
Example #9
Source File: UUDecoder.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decode a UU atom. Note that if l is less than 3 we don't write
 * the extra bits, however the encoder always encodes 4 character
 * groups even when they are not needed.
 */
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l)
    throws IOException {
    int i, c1, c2, c3, c4;
    int a, b, c;
    StringBuffer x = new StringBuffer();

    for (i = 0; i < 4; i++) {
        c1 = inStream.read();
        if (c1 == -1) {
            throw new CEStreamExhausted();
        }
        x.append((char)c1);
        decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f);
    }
    a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3);
    b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf);
    c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f);
    outStream.write((byte)(a & 0xff));
    if (l > 1) {
        outStream.write((byte)( b & 0xff));
    }
    if (l > 2) {
        outStream.write((byte)(c&0xff));
    }
}
 
Example #10
Source File: UUDecoder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the end of the line for the next operation.
 * The following sequences are recognized as end-of-line
 * CR, CR LF, or LF
 */
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
    int c;
    while (true) {
        c = inStream.read();
        if (c == -1) {
            throw new CEStreamExhausted();
        }
        if (c == '\n') {
            break;
        }
        if (c == '\r') {
            c = inStream.read();
            if ((c != '\n') && (c != -1)) {
                inStream.unread (c);
            }
            break;
        }
    }
}
 
Example #11
Source File: AsyncInputStream.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an instance of {@link org.wisdom.framework.vertx.AsyncInputStream}.
 *
 * @param vertx     the Vert.X instance
 * @param executor  the executor used to read the chunk
 * @param in        the input stream to read
 * @param chunkSize the chunk size
 */
public AsyncInputStream(Vertx vertx, ExecutorService executor, InputStream in, int chunkSize) {
    if (in == null) {
        throw new NullPointerException("in");
    }
    if (vertx == null) {
        throw new NullPointerException("vertx");
    }
    this.vertx = vertx;
    if (chunkSize <= 0) {
        throw new IllegalArgumentException(
                "chunkSize: " + chunkSize +
                        " (expected: a positive integer)");
    }

    if (in instanceof PushbackInputStream) {
        this.in = (PushbackInputStream) in;
    } else {
        this.in = new PushbackInputStream(in);
    }
    this.chunkSize = chunkSize;
    this.executor = executor;
}
 
Example #12
Source File: UUDecoder.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the end of the line for the next operation.
 * The following sequences are recognized as end-of-line
 * CR, CR LF, or LF
 */
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
    int c;
    while (true) {
        c = inStream.read();
        if (c == -1) {
            throw new CEStreamExhausted();
        }
        if (c == '\n') {
            break;
        }
        if (c == '\r') {
            c = inStream.read();
            if ((c != '\n') && (c != -1)) {
                inStream.unread (c);
            }
            break;
        }
    }
}
 
Example #13
Source File: CMapParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void parseBegincidchar(Number cosCount, PushbackInputStream cmapStream, CMap result) throws IOException
{
    for (int j = 0; j < cosCount.intValue(); j++)
    {
        Object nextToken = parseNextToken(cmapStream);
        if (nextToken instanceof Operator)
        {
            if (!((Operator) nextToken).op.equals("endcidchar"))
            {
                throw new IOException("Error : ~cidchar contains an unexpected operator : "
                        + ((Operator) nextToken).op);
            }
            break;
        }
        byte[] inputCode = (byte[]) nextToken;
        int mappedCode = (Integer) parseNextToken(cmapStream);
        int mappedCID = createIntFromBytes(inputCode);
        result.addCIDMapping(mappedCode, mappedCID);
    }
}
 
Example #14
Source File: UUDecoder.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the end of the line for the next operation.
 * The following sequences are recognized as end-of-line
 * CR, CR LF, or LF
 */
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
    int c;
    while (true) {
        c = inStream.read();
        if (c == -1) {
            throw new CEStreamExhausted();
        }
        if (c == '\n') {
            break;
        }
        if (c == '\r') {
            c = inStream.read();
            if ((c != '\n') && (c != -1)) {
                inStream.unread (c);
            }
            break;
        }
    }
}
 
Example #15
Source File: ChunkedStream.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance that fetches data from the specified stream.创建一个新实例,该实例从指定的流中获取数据。
 *
 * @param chunkSize the number of bytes to fetch on each
 *                  {@link #readChunk(ChannelHandlerContext)} call
 */
public ChunkedStream(InputStream in, int chunkSize) {
    if (in == null) {
        throw new NullPointerException("in");
    }
    if (chunkSize <= 0) {
        throw new IllegalArgumentException(
                "chunkSize: " + chunkSize +
                " (expected: a positive integer)");
    }

    if (in instanceof PushbackInputStream) {
        this.in = (PushbackInputStream) in;
    } else {
        this.in = new PushbackInputStream(in);
    }
    this.chunkSize = chunkSize;
}
 
Example #16
Source File: CMapParser.java    From PdfBox-Android with Apache License 2.0 6 votes vote down vote up
private void parseBegincodespacerange(Object previousToken, PushbackInputStream cmapStream, CMap result) throws IOException
{
    Number cosCount = (Number) previousToken;
    for (int j = 0; j < cosCount.intValue(); j++)
    {
        Object nextToken = parseNextToken(cmapStream);
        if (nextToken instanceof Operator)
        {
            if (!((Operator) nextToken).op.equals("endcodespacerange"))
            {
                throw new IOException("Error : ~codespacerange contains an unexpected operator : "
                        + ((Operator) nextToken).op);
            }
            break;
        }
        byte[] startRange = (byte[]) nextToken;
        byte[] endRange = (byte[]) parseNextToken(cmapStream);
        CodespaceRange range = new CodespaceRange();
        range.setStart(startRange);
        range.setEnd(endRange);
        result.addCodespaceRange(range);
    }
}
 
Example #17
Source File: UUDecoder.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decode a UU atom. Note that if l is less than 3 we don't write
 * the extra bits, however the encoder always encodes 4 character
 * groups even when they are not needed.
 */
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l)
    throws IOException {
    int i, c1, c2, c3, c4;
    int a, b, c;
    StringBuffer x = new StringBuffer();

    for (i = 0; i < 4; i++) {
        c1 = inStream.read();
        if (c1 == -1) {
            throw new CEStreamExhausted();
        }
        x.append((char)c1);
        decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f);
    }
    a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3);
    b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf);
    c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f);
    outStream.write((byte)(a & 0xff));
    if (l > 1) {
        outStream.write((byte)( b & 0xff));
    }
    if (l > 2) {
        outStream.write((byte)(c&0xff));
    }
}
 
Example #18
Source File: HttpResponse.java    From nutch-selenium with Apache License 2.0 6 votes vote down vote up
private int parseStatusLine(PushbackInputStream in, StringBuffer line) throws IOException, HttpException {
    readLine(in, line, false);

    int codeStart = line.indexOf(" ");
    int codeEnd = line.indexOf(" ", codeStart + 1);

    // handle lines with no plaintext result code, ie:
    // "HTTP/1.1 200" vs "HTTP/1.1 200 OK"
    if (codeEnd == -1)
        codeEnd = line.length();

    int code;
    try {
        code = Integer.parseInt(line.substring(codeStart + 1, codeEnd));
    } catch (NumberFormatException e) {
        throw new HttpException("bad status line '" + line + "': " + e.getMessage(), e);
    }

    return code;
}
 
Example #19
Source File: UCDecoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * this method reads the CRC that is at the end of every line and
 * verifies that it matches the computed CRC.
 *
 * @exception CEFormatException if CRC check fails.
 */
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
    int i;
    int lineCRC = crc.value;
    int readCRC;
    byte tmp[];

    lineAndSeq.reset();
    decodeAtom(inStream, lineAndSeq, 2);
    tmp = lineAndSeq.toByteArray();
    readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
    if (readCRC != lineCRC) {
        throw new CEFormatException("UCDecoder: CRC check failed.");
    }
}
 
Example #20
Source File: UUDecoder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * UUencoded files have a buffer suffix which consists of the word
 * end. This line should immediately follow the line with a single
 * space in it.
 */
protected void decodeBufferSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException  {
    int     c;

    c = inStream.read(decoderBuffer);
    if ((decoderBuffer[0] != 'e') || (decoderBuffer[1] != 'n') ||
        (decoderBuffer[2] != 'd')) {
        throw new CEFormatException("UUDecoder: Missing 'end' line.");
    }
}
 
Example #21
Source File: CompactParseable.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static private String detectEncoding(PushbackInputStream in) throws IOException {
  String encoding = UTF8;
  int b1 = in.read();
  if (b1 != -1) {
    int b2 = in.read();
    if (b2 != -1) {
      in.unread(b2);
      if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF))
        encoding = UTF16;
    }
    in.unread(b1);
  }
  return encoding;
}
 
Example #22
Source File: CharacterDecoder.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decode the text from the InputStream and write the decoded
 * octets to the OutputStream. This method runs until the stream
 * is exhausted.
 * @exception CEFormatException An error has occurred while decoding
 * @exception CEStreamExhausted The input stream is unexpectedly out of data
 */
public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
    int     i;
    int     totalBytes = 0;

    PushbackInputStream ps = new PushbackInputStream (aStream);
    decodeBufferPrefix(ps, bStream);
    while (true) {
        int length;

        try {
            length = decodeLinePrefix(ps, bStream);
            for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            }
            if ((i + bytesPerAtom()) == length) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            } else {
                decodeAtom(ps, bStream, length - i);
                totalBytes += (length - i);
            }
            decodeLineSuffix(ps, bStream);
        } catch (CEStreamExhausted e) {
            break;
        }
    }
    decodeBufferSuffix(ps, bStream);
}
 
Example #23
Source File: UCDecoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * decodeLinePrefix reads the sequence number and the number of
 * encoded bytes from the line. If the sequence number is not the
 * previous sequence number + 1 then an exception is thrown.
 * UCE lines are line terminator immune, they all start with *
 * so the other thing this method does is scan for the next line
 * by looking for the * character.
 *
 * @exception CEFormatException out of sequence lines detected.
 */
protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream)  throws IOException {
    int     i;
    int     nLen, nSeq;
    byte    xtmp[];
    int     c;

    crc.value = 0;
    while (true) {
        c = inStream.read(tmp, 0, 1);
        if (c == -1) {
            throw new CEStreamExhausted();
        }
        if (tmp[0] == '*') {
            break;
        }
    }
    lineAndSeq.reset();
    decodeAtom(inStream, lineAndSeq, 2);
    xtmp = lineAndSeq.toByteArray();
    nLen = xtmp[0] & 0xff;
    nSeq = xtmp[1] & 0xff;
    if (nSeq != sequence) {
        throw new CEFormatException("UCDecoder: Out of sequence line.");
    }
    sequence = (sequence + 1) & 0xff;
    return (nLen);
}
 
Example #24
Source File: UUDecoder.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * UUencoded files have a buffer suffix which consists of the word
 * end. This line should immediately follow the line with a single
 * space in it.
 */
protected void decodeBufferSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException  {
    int     c;

    c = inStream.read(decoderBuffer);
    if ((decoderBuffer[0] != 'e') || (decoderBuffer[1] != 'n') ||
        (decoderBuffer[2] != 'd')) {
        throw new CEFormatException("UUDecoder: Missing 'end' line.");
    }
}
 
Example #25
Source File: CompactParseable.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static private String detectEncoding(PushbackInputStream in) throws IOException {
  String encoding = UTF8;
  int b1 = in.read();
  if (b1 != -1) {
    int b2 = in.read();
    if (b2 != -1) {
      in.unread(b2);
      if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF))
        encoding = UTF16;
    }
    in.unread(b1);
  }
  return encoding;
}
 
Example #26
Source File: UCDecoder.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * this method reads the CRC that is at the end of every line and
 * verifies that it matches the computed CRC.
 *
 * @exception CEFormatException if CRC check fails.
 */
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
    int i;
    int lineCRC = crc.value;
    int readCRC;
    byte tmp[];

    lineAndSeq.reset();
    decodeAtom(inStream, lineAndSeq, 2);
    tmp = lineAndSeq.toByteArray();
    readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
    if (readCRC != lineCRC) {
        throw new CEFormatException("UCDecoder: CRC check failed.");
    }
}
 
Example #27
Source File: CharacterDecoder.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decode the text from the InputStream and write the decoded
 * octets to the OutputStream. This method runs until the stream
 * is exhausted.
 * @exception CEFormatException An error has occurred while decoding
 * @exception CEStreamExhausted The input stream is unexpectedly out of data
 */
public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
    int     i;
    int     totalBytes = 0;

    PushbackInputStream ps = new PushbackInputStream (aStream);
    decodeBufferPrefix(ps, bStream);
    while (true) {
        int length;

        try {
            length = decodeLinePrefix(ps, bStream);
            for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            }
            if ((i + bytesPerAtom()) == length) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            } else {
                decodeAtom(ps, bStream, length - i);
                totalBytes += (length - i);
            }
            decodeLineSuffix(ps, bStream);
        } catch (CEStreamExhausted e) {
            break;
        }
    }
    decodeBufferSuffix(ps, bStream);
}
 
Example #28
Source File: UCDecoder.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * decodeLinePrefix reads the sequence number and the number of
 * encoded bytes from the line. If the sequence number is not the
 * previous sequence number + 1 then an exception is thrown.
 * UCE lines are line terminator immune, they all start with *
 * so the other thing this method does is scan for the next line
 * by looking for the * character.
 *
 * @exception CEFormatException out of sequence lines detected.
 */
protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream)  throws IOException {
    int     i;
    int     nLen, nSeq;
    byte    xtmp[];
    int     c;

    crc.value = 0;
    while (true) {
        c = inStream.read(tmp, 0, 1);
        if (c == -1) {
            throw new CEStreamExhausted();
        }
        if (tmp[0] == '*') {
            break;
        }
    }
    lineAndSeq.reset();
    decodeAtom(inStream, lineAndSeq, 2);
    xtmp = lineAndSeq.toByteArray();
    nLen = xtmp[0] & 0xff;
    nSeq = xtmp[1] & 0xff;
    if (nSeq != sequence) {
        throw new CEFormatException("UCDecoder: Out of sequence line.");
    }
    sequence = (sequence + 1) & 0xff;
    return (nLen);
}
 
Example #29
Source File: RExpParser.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private InputStream init(PushbackInputStream is) throws IOException {
	byte[] gzipMagic = new byte[2];

	ByteStreams.readFully(is, gzipMagic);

	is.unread(gzipMagic);

	if(Arrays.equals(RExpParser.GZIP_MAGIC, gzipMagic)){
		return new GZIPInputStream(is);
	}

	return is;
}
 
Example #30
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}