Java Code Examples for java.io.PushbackInputStream

The following examples show how to use java.io.PushbackInputStream. These examples are extracted from open source projects. 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
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 2
/**
 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 3
Source Project: nutch-htmlunit   Source File: HttpResponse.java    License: 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 4
Source Project: openjdk-8-source   Source File: UUDecoder.java    License: 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 Project: spork   Source File: Utf8StorageConverter.java    License: 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 6
Source Project: dragonwell8_jdk   Source File: UUDecoder.java    License: 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 7
Source Project: nutch-selenium   Source File: HttpResponse.java    License: 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 Project: gcs   Source File: CMapParser.java    License: 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 9
Source Project: openjdk-jdk8u   Source File: UUDecoder.java    License: 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 10
Source Project: netty-4.1.22   Source File: ChunkedStream.java    License: 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 11
Source Project: PdfBox-Android   Source File: CMapParser.java    License: 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 12
Source Project: james-project   Source File: CachedBlobStore.java    License: 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 13
/**
 * 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 14
Source Project: netbeans   Source File: DefaultAttributes.java    License: 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 15
Source Project: jdk8u_jdk   Source File: UUDecoder.java    License: 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 16
Source Project: jdk8u_jdk   Source File: UUDecoder.java    License: 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 17
Source Project: wisdom   Source File: AsyncInputStream.java    License: 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 18
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 19
Source Project: hottub   Source File: CharacterDecoder.java    License: 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 20
Source Project: openjdk-8   Source File: UCDecoder.java    License: 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 21
Source Project: uavstack   Source File: CharacterDecoderUrl.java    License: Apache License 2.0 5 votes vote down vote up
public void decodeBuffer(InputStream inputstream, OutputStream outputstream) throws IOException {

        @SuppressWarnings("unused")
        int j = 0;
        PushbackInputStream pushbackinputstream = new PushbackInputStream(inputstream);
        decodeBufferPrefix(pushbackinputstream, outputstream);
        try {
            do {
                int k = decodeLinePrefix(pushbackinputstream, outputstream);
                int i;
                for (i = 0; i + bytesPerAtom() < k; i += bytesPerAtom()) {
                    decodeAtom(pushbackinputstream, outputstream, bytesPerAtom());
                    j += bytesPerAtom();
                }

                if (i + bytesPerAtom() == k) {
                    decodeAtom(pushbackinputstream, outputstream, bytesPerAtom());
                    j += bytesPerAtom();
                }
                else {
                    decodeAtom(pushbackinputstream, outputstream, k - i);
                    j += k - i;
                }
                decodeLineSuffix(pushbackinputstream, outputstream);
            }
            while (true);
        }
        catch (IOException cestreamexhausted) {
            decodeBufferSuffix(pushbackinputstream, outputstream);
        }
    }
 
Example 22
Source Project: jdk8u_jdk   Source File: UCDecoder.java    License: 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 23
Source Project: gcs   Source File: CMapParser.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private void parseBeginbfchar(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("endbfchar"))
            {
                throw new IOException("Error : ~bfchar contains an unexpected operator : "
                        + ((Operator) nextToken).op);
            }
            break;
        }
        byte[] inputCode = (byte[]) nextToken;
        nextToken = parseNextToken(cmapStream);
        if (nextToken instanceof byte[])
        {
            byte[] bytes = (byte[]) nextToken;
            String value = createStringFromBytes(bytes);
            result.addCharMapping(inputCode, value);
        }
        else if (nextToken instanceof LiteralName)
        {
            result.addCharMapping(inputCode, ((LiteralName) nextToken).name);
        }
        else
        {
            throw new IOException("Error parsing CMap beginbfchar, expected{COSString "
                    + "or COSName} and not " + nextToken);
        }
    }
}
 
Example 24
Source Project: cxf   Source File: AsyncHTTPConduit.java    License: Apache License 2.0 5 votes vote down vote up
protected InputStream getPartialResponse() throws IOException {
    InputStream in = null;
    int responseCode = getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_ACCEPTED
        || responseCode == HttpURLConnection.HTTP_OK) {

        Header head = httpResponse.getFirstHeader(HttpHeaderHelper.CONTENT_LENGTH);
        int cli = 0;
        if (head != null) {
            cli = Integer.parseInt(head.getValue());
        }
        head = httpResponse.getFirstHeader(HttpHeaderHelper.TRANSFER_ENCODING);
        boolean isChunked = head != null &&  HttpHeaderHelper.CHUNKED.equalsIgnoreCase(head.getValue());
        head = httpResponse.getFirstHeader(HttpHeaderHelper.CONNECTION);
        boolean isEofTerminated = head != null &&  HttpHeaderHelper.CLOSE.equalsIgnoreCase(head.getValue());
        if (cli > 0) {
            in = getInputStream();
        } else if (isChunked || isEofTerminated) {
            // ensure chunked or EOF-terminated response is non-empty
            try {
                PushbackInputStream pin =
                    new PushbackInputStream(getInputStream());
                int c = pin.read();
                if (c != -1) {
                    pin.unread((byte)c);
                    in = pin;
                }
            } catch (IOException ioe) {
                // ignore
            }
        }
    }
    return in;
}
 
Example 25
Source Project: dragonwell8_jdk   Source File: UCDecoder.java    License: 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 26
Source Project: gpx-animator   Source File: GpxParser.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("PMD.CloseResource") // The returned stream will be used and closed in a try-with-resources block
public static InputStream decompressStream(final InputStream input) throws IOException {
    final PushbackInputStream pb = new PushbackInputStream(input, 2);
    final byte[] signature = new byte[2];
    final int numBytesRead = pb.read(signature);
    pb.unread(signature, 0, numBytesRead);
    return signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b ? new GZIPInputStream(pb) : pb;
}
 
Example 27
Source Project: itext2   Source File: RtfCtrlWordMgr.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
	 * Constructor
	 * @param rtfParser The parser object this manager works with.
	 * @param reader the PushbackReader from the tokeniser.
	 */
	public RtfCtrlWordMgr(RtfParser rtfParser, PushbackInputStream reader) {
		ctrlWordMap = new RtfCtrlWordMap(rtfParser);
		
//		// TIMING DEBUG INFO
//		endFree = Runtime.getRuntime().freeMemory();
//		endTime = System.currentTimeMillis();
//		endDate = new Date();
//		System.out.println("RtfCtrlWordMgr start date: " + startDate.toLocaleString());
//		System.out.println("RtfCtrlWordMgr end date  : " + endDate.toLocaleString());
//		System.out.println("  Elapsed time    : " + Long.toString(endTime - startTime) + " milliseconds.");
//		System.out.println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.format(startFree / 1024) + "k");
//		System.out.println("End Constructor RtfCtrlWordMgr , free mem is " + df.format(endFree / 1024) + "k");
//        System.out.println("RtfCtrlWordMgr used approximately " + df.format((startFree - endFree) / 1024) + "k");
	}
 
Example 28
Source Project: dragonwell8_jdk   Source File: UUDecoder.java    License: 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 29
Source Project: jdk8u_jdk   Source File: CharacterDecoder.java    License: 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 30
Source Project: TencentKona-8   Source File: UCDecoder.java    License: 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);
}