Java Code Examples for org.springframework.core.io.buffer.DataBuffer#writePosition()

The following examples show how to use org.springframework.core.io.buffer.DataBuffer#writePosition() . 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 Project: spring-analysis-note   File: StringDecoder.java    License: MIT License 6 votes vote down vote up
/**
 * Find the given delimiter in the given data buffer.
 * @return the index of the delimiter, or -1 if not found.
 */
private static int indexOf(DataBuffer buffer, byte[] delimiter) {
	for (int i = buffer.readPosition(); i < buffer.writePosition(); i++) {
		int bufferPos = i;
		int delimiterPos = 0;
		while (delimiterPos < delimiter.length) {
			if (buffer.getByte(bufferPos) != delimiter[delimiterPos]) {
				break;
			}
			else {
				bufferPos++;
				boolean endOfBuffer = bufferPos == buffer.writePosition();
				boolean endOfDelimiter = delimiterPos == delimiter.length - 1;
				if (endOfBuffer && !endOfDelimiter) {
					return -1;
				}
			}
			delimiterPos++;
		}
		if (delimiterPos == delimiter.length) {
			return i - buffer.readPosition();
		}
	}
	return -1;
}
 
Example 2
Source Project: java-technology-stack   File: StringDecoder.java    License: MIT License 6 votes vote down vote up
/**
 * Find the given delimiter in the given data buffer.
 * @return the index of the delimiter, or -1 if not found.
 */
private static int indexOf(DataBuffer dataBuffer, byte[] delimiter) {
	for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
		int dataBufferPos = i;
		int delimiterPos = 0;
		while (delimiterPos < delimiter.length) {
			if (dataBuffer.getByte(dataBufferPos) != delimiter[delimiterPos]) {
				break;
			}
			else {
				dataBufferPos++;
				if (dataBufferPos == dataBuffer.writePosition() &&
						delimiterPos != delimiter.length - 1) {
					return -1;
				}
			}
			delimiterPos++;
		}
		if (delimiterPos == delimiter.length) {
			return i - dataBuffer.readPosition();
		}
	}
	return -1;
}
 
Example 3
@Override
protected DataBuffer readFromInputStream() throws IOException {
	boolean release = true;
	int capacity = this.bufferSize;
	DataBuffer dataBuffer = this.factory.allocateBuffer(capacity);
	try {
		ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, capacity);
		ServletRequest request = getNativeRequest();
		int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
		logBytesRead(read);
		if (read > 0) {
			dataBuffer.writePosition(read);
			release = false;
			return dataBuffer;
		}
		else if (read == -1) {
			return EOF_BUFFER;
		}
		else {
			return null;
		}
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
}
 
Example 4
private static String toString(DataBuffer dataBuffer) {
	byte[] bytes = new byte[dataBuffer.readableByteCount()];
	int j = 0;
	for (int i = dataBuffer.readPosition(); i < dataBuffer.writePosition(); i++) {
		bytes[j++] = dataBuffer.getByte(i);
	}
	return toString(bytes);
}
 
Example 5
@Override
protected DataBuffer readFromInputStream() throws IOException {
	boolean release = true;
	int capacity = this.bufferSize;
	DataBuffer dataBuffer = this.factory.allocateBuffer(capacity);
	try {
		ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, capacity);
		ServletRequest request = getNativeRequest();
		int read = ((CoyoteInputStream) request.getInputStream()).read(byteBuffer);
		logBytesRead(read);
		if (read > 0) {
			dataBuffer.writePosition(read);
			release = false;
			return dataBuffer;
		}
		else if (read == -1) {
			return EOF_BUFFER;
		}
		else {
			return null;
		}
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
}