本文整理汇总了Java中org.apache.harmony.nio.AddressUtil类的典型用法代码示例。如果您正苦于以下问题:Java AddressUtil类的具体用法?Java AddressUtil怎么用?Java AddressUtil使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AddressUtil类属于org.apache.harmony.nio包,在下文中一共展示了AddressUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int writeImpl(ByteBuffer buf) throws IOException {
synchronized (writeLock) {
int result = 0;
try {
begin();
int length = buf.remaining();
int start = buf.position();
if (buf.isDirect()) {
int address = AddressUtil.getDirectBufferAddress(buf);
result = networkSystem.sendDirect(fd, address, start, length, 0, null);
} else {
// buf is assured to have array.
start += buf.arrayOffset();
result = networkSystem.send(fd, buf.array(), start, length, 0, null);
}
return result;
} finally {
end(result > 0);
}
}
}
示例2: receiveDirectImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop) throws IOException {
SocketAddress retAddr = null;
DatagramPacket receivePacket = new DatagramPacket(stubArray, 0);
int oldposition = target.position();
int received = 0;
do {
int address = AddressUtil.getDirectBufferAddress(target);
received = networkSystem.recvDirect(fd, receivePacket, address,
target.position(), target.remaining(), false, isConnected());
// security check
SecurityManager sm = System.getSecurityManager();
if (!isConnected() && null != sm) {
try {
sm.checkAccept(receivePacket.getAddress().getHostAddress(),
receivePacket.getPort());
} catch (SecurityException e) {
// do discard the datagram packet
receivePacket = null;
}
}
if (null != receivePacket && null != receivePacket.getAddress()) {
// copy the data of received packet
if (received > 0) {
target.position(oldposition + received);
}
retAddr = receivePacket.getSocketAddress();
break;
}
} while (loop);
return retAddr;
}
示例3: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int readImpl(ByteBuffer readBuffer) throws IOException {
synchronized (readLock) {
int readCount = 0;
try {
begin();
int start = readBuffer.position();
int length = readBuffer.remaining();
if (readBuffer.isDirect()) {
int address = AddressUtil.getDirectBufferAddress(readBuffer);
readCount = networkSystem.recvDirect(fd, null, address, start, length,
false, isConnected());
} else {
// the target is assured to have array.
byte[] target = readBuffer.array();
start += readBuffer.arrayOffset();
readCount = networkSystem.recv(fd, null, target, start, length, false,
isConnected());
}
return readCount;
} catch (InterruptedIOException e) {
// InterruptedIOException will be thrown when timeout.
return 0;
} finally {
end(readCount > 0);
}
}
}
示例4: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
/**
* Read from channel, and store the result in the target.
*
* @param target
* output parameter
*/
private int readImpl(ByteBuffer target) throws IOException {
synchronized (readLock) {
int readCount = 0;
try {
if (isBlocking()) {
begin();
}
int offset = target.position();
int length = target.remaining();
if (target.isDirect()) {
// BEGIN android-changed
// changed address from long to int
int address = AddressUtil.getDirectBufferAddress(target);
readCount = networkSystem.readDirect(fd, address + offset, length);
// END android-changed
} else {
// target is assured to have array.
byte[] array = target.array();
offset += target.arrayOffset();
readCount = networkSystem.read(fd, array, offset, length);
}
return readCount;
} finally {
if (isBlocking()) {
end(readCount > 0);
}
}
}
}
示例5: writeImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int writeImpl(ByteBuffer buf) throws IOException {
synchronized (writeLock) {
int result = 0;
try {
begin();
int length = buf.remaining();
int start = buf.position();
if (buf.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(buf);
result = networkSystem.sendConnectedDatagramDirect(fd,
address, start, length, isBound);
} else {
// buf is assured to have array.
start += buf.arrayOffset();
result = networkSystem.sendConnectedDatagram(fd, buf
.array(), start, length, isBound);
}
return result;
} catch (SocketException e) {
if (e.getCause() instanceof ErrorCodeException) {
if (ERRCODE_SOCKET_NONBLOCKING_WOULD_BLOCK == ((ErrorCodeException) e
.getCause()).getErrorCode()) {
return result;
}
}
throw e;
} finally {
end(result > 0);
}
}
}
示例6: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
/**
* Read from channel, and store the result in the target.
*
* @param target
* output parameter
*/
private int readImpl(ByteBuffer target) throws IOException {
synchronized (readLock) {
int readCount = 0;
try {
if (isBlocking()) {
begin();
}
int offset = target.position();
int length = target.remaining();
if (target.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(target);
readCount = networkSystem.readDirect(fd, address + offset,
length, (isBlocking() ? TIMEOUT_BLOCK
: TIMEOUT_NONBLOCK));
} else {
// target is assured to have array.
byte[] array = target.array();
offset += target.arrayOffset();
readCount = networkSystem.read(fd, array, offset, length,
(isBlocking() ? TIMEOUT_BLOCK : TIMEOUT_NONBLOCK));
}
return readCount;
} finally {
if (isBlocking()) {
end(readCount > 0);
}
}
}
}
示例7: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int readImpl(ByteBuffer readBuffer) throws IOException {
synchronized(readLock){
int readCount = 0;
try {
begin();
// timeout == 0 means block read.
// DEFAULT_TIMEOUT is used in non-block mode.
int timeout = isBlocking() ? 0 : DEFAULT_TIMEOUT;
int start = readBuffer.position();
int length = readBuffer.remaining();
if (readBuffer.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(readBuffer);
if (isConnected()) {
readCount = networkSystem.recvConnectedDatagramDirect(fd,
null, address, start, length, timeout, false);
} else {
readCount = networkSystem.receiveDatagramDirect(fd,
null, address, start, length, timeout, false);
}
} else {
// the target is assured to have array.
byte[] target = readBuffer.array();
start += readBuffer.arrayOffset();
if (isConnected()) {
readCount = networkSystem.recvConnectedDatagram(fd, null,
target, start, length, timeout, false);
} else {
readCount = networkSystem.receiveDatagram(fd, null, target,
start, length, timeout, false);
}
}
return readCount;
} catch (InterruptedIOException e) {
// InterruptedIOException will be thrown when timeout.
return 0;
} finally {
end(readCount > 0);
}
}
}
示例8: writeImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int writeImpl(ByteBuffer buf) throws IOException {
synchronized(writeLock){
int result = 0;
try {
begin();
int length = buf.remaining();
int start = buf.position();
if (buf.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(buf);
result = networkSystem.sendConnectedDatagramDirect(fd, address,
start, length, isBound);
} else {
// buf is assured to have array.
start += buf.arrayOffset();
result = networkSystem.sendConnectedDatagram(fd, buf.array(),
start, length, isBound);
}
return result;
} catch (SocketException e) {
if (e.getCause() instanceof ErrorCodeException) {
if (ERRCODE_SOCKET_NONBLOCKING_WOULD_BLOCK == ((ErrorCodeException) e
.getCause()).getErrorCode()) {
return result;
}
}
throw e;
} finally {
end(result > 0);
}
}
}
示例9: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int readImpl(ByteBuffer target) throws IOException {
synchronized(readLock){
int readCount = 0;
try {
if (isBlocking()) {
begin();
}
int offset = target.position();
int length = target.remaining();
if (target.isDirect()) {
long address = AddressUtil.getDirectBufferAddress(target);
readCount = networkSystem.readDirect(fd, address, offset,
length, (isBlocking() ? TIMEOUT_BLOCK
: TIMEOUT_NONBLOCK));
} else {
// target is assured to have array.
byte[] array = target.array();
offset += target.arrayOffset();
readCount = networkSystem.read(fd, array, offset, length,
(isBlocking() ? TIMEOUT_BLOCK : TIMEOUT_NONBLOCK));
}
return readCount;
} finally {
if (isBlocking()) {
end(readCount > 0);
}
}
}
}
示例10: send
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
/**
* @see java.nio.channels.DatagramChannel#send(java.nio.ByteBuffer,
* java.net.SocketAddress)
*/
@Override
public int send(ByteBuffer source, SocketAddress socketAddress) throws IOException {
// must not null
checkNotNull(source);
// must open
checkOpen();
// transfer socketAddress
InetSocketAddress isa = (InetSocketAddress) socketAddress;
if (null == isa.getAddress()) {
throw new IOException();
}
if (isConnected()) {
if (!connectAddress.equals(isa)) {
throw new IllegalArgumentException();
}
} else {
// not connected, check security
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
if (isa.getAddress().isMulticastAddress()) {
sm.checkMulticast(isa.getAddress());
} else {
sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort());
}
}
}
// the return value.
int sendCount = 0;
try {
begin();
byte[] array = null;
int length = source.remaining();
int oldposition = source.position();
int start = oldposition;
if (source.isDirect()) {
synchronized (writeLock) {
int data_address = AddressUtil.getDirectBufferAddress(source);
sendCount = networkSystem.sendDirect(fd, data_address, start, length,
isa.getPort(), isa.getAddress());
}
} else {
if (source.hasArray()) {
array = source.array();
start += source.arrayOffset();
} else {
array = new byte[length];
source.get(array);
start = 0;
}
synchronized (writeLock) {
sendCount = networkSystem.send(fd, array, start, length,
isa.getPort(), isa.getAddress());
}
}
source.position(oldposition + sendCount);
return sendCount;
} finally {
end(sendCount >= 0);
}
}
示例11: decodeLoop
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb){
int cbRemaining = cb.remaining();
if(CharsetProviderImpl.hasLoadedNatives() && bb.isDirect() && bb.hasRemaining() && cb.hasArray()){
int cbPos = cb.position();
int bbPos = bb.position();
int[] res = {bb.remaining(), cbRemaining, 0, 0};
nDecode(cb.array(), cb.arrayOffset()+cbPos, bb.remaining(), AddressUtil.getDirectBufferAddress(bb), bbPos, res);
bb.position(bbPos+bb.remaining()-res[0]);
cb.position(cbPos+cbRemaining-res[1]);
if( res[2] != 0 ) {
return CoderResult.unmappableForLength(res[2]);
} else {
if(res[3]!=0) return CoderResult.OVERFLOW;
}
}else{
while(bb.hasRemaining()){
if( cbRemaining == 0 ) return CoderResult.OVERFLOW;
int in = (int)bb.get();
if(in < 0 && in >= -114){
int index = decodeIndex[in&0xFF];
if(index < 0 && arrDecode0.charAt(in&0xFF) == '\u0000' || !bb.hasRemaining()) {
if(arrDecode0.charAt(in&0xFF) == '\u0000') {
bb.position(bb.position() - 1);
if(index > 0 && bb.remaining()==1) {
return CoderResult.UNDERFLOW;
}
return CoderResult.unmappableForLength(1);
} else {
cb.put(arrDecode0.charAt(in&0xFF));
return CoderResult.UNDERFLOW;
}
}
int variable = 0;
byte b2 = 0;
if (index < 0) { variable = 0; index = in&0xFF; } else{
variable = index >> 4;
index <<= 8;
b2 = bb.get();
index += (b2 & 0xFF);
index = index - (variable << 12);
}
char resultChar = '\u0000';
switch(variable) {
case 0:
resultChar = arrDecode0.charAt(index);
break;
case 1:
resultChar = arrDecode1.charAt(index);
break;
case 2:
resultChar = arrDecode2.charAt(index);
break;
case 3:
resultChar = arrDecode3.charAt(index);
break;
case 4:
resultChar = arrDecode4.charAt(index);
break;
case 5:
resultChar = arrDecode5.charAt(index);
break;
}
if (resultChar != '\u0000') {
cb.put(resultChar);
} else {
bb.position(bb.position() - 2);
if(arrDecode0.charAt(b2 & 0xFF) == '\u0000') {
return CoderResult.unmappableForLength(2);
} else {
return CoderResult.unmappableForLength(1);
}
}
}else {
cb.put((char)(in & 0xFF));
}
cbRemaining--;
}
}
return CoderResult.UNDERFLOW;
}
示例12: decodeLoop
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb){
int cbRemaining = cb.remaining();
if(CharsetProviderImpl.hasLoadedNatives() && bb.isDirect() && bb.hasRemaining() && cb.hasArray()){
int cbPos = cb.position();
int bbPos = bb.position();
int[] res = {bb.remaining(), cbRemaining, 0, 0};
nDecode(cb.array(), cb.arrayOffset()+cbPos, bb.remaining(), AddressUtil.getDirectBufferAddress(bb), bbPos, res);
bb.position(bbPos+bb.remaining()-res[0]);
cb.position(cbPos+cbRemaining-res[1]);
if( res[2] != 0 ) {
return CoderResult.unmappableForLength(res[2]);
} else {
if(res[3]!=0) return CoderResult.OVERFLOW;
}
}else{
while(bb.hasRemaining()){
if( cbRemaining == 0 ) return CoderResult.OVERFLOW;
char in = (char)(bb.get() & 0xFF);
if(in >= 26){
int index = decodeIndex[in&0xFF];
if(index < 0 && arrDecode0.charAt(in&0xFF) == '\u0000' || !bb.hasRemaining()) {
if(arrDecode0.charAt(in&0xFF) == '\u0000') {
bb.position(bb.position() - 1);
if(index > 0 && bb.remaining()==1) {
return CoderResult.UNDERFLOW;
}
return CoderResult.unmappableForLength(1);
} else {
cb.put(arrDecode0.charAt(in&0xFF));
return CoderResult.UNDERFLOW;
}
}
int variable = 0;
byte b2 = 0;
if (index < 0) { variable = 0; index = in&0xFF; } else{
variable = index >> 4;
index <<= 8;
b2 = bb.get();
index += (b2 & 0xFF);
index = index - (variable << 12);
}
char resultChar = '\u0000';
switch(variable) {
case 0:
resultChar = arrDecode0.charAt(index);
break;
case 1:
resultChar = arrDecode1.charAt(index);
break;
case 2:
resultChar = arrDecode2.charAt(index);
break;
case 3:
resultChar = arrDecode3.charAt(index);
break;
}
if (resultChar != '\u0000') {
cb.put(resultChar);
} else {
bb.position(bb.position() - 2);
if(arrDecode0.charAt(b2 & 0xFF) == '\u0000') {
return CoderResult.unmappableForLength(2);
} else {
return CoderResult.unmappableForLength(1);
}
}
}else {
cb.put((char)(in & 0xFF));
}
cbRemaining--;
}
}
return CoderResult.UNDERFLOW;
}
示例13: receiveDirectImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop)
throws IOException {
SocketAddress retAddr = null;
DatagramPacket receivePacket = new DatagramPacket(stubArray, 0);
int oldposition = target.position();
int received = 0;
do {
long address = AddressUtil.getDirectBufferAddress(target);
if (isConnected()) {
received = networkSystem.recvConnectedDatagramDirect(fd,
receivePacket, address, target.position(), target
.remaining(), isBlocking() ? 0
: DEFAULT_TIMEOUT, false);
} else {
received = networkSystem.receiveDatagramDirect(fd,
receivePacket, address, target.position(), target
.remaining(), isBlocking() ? 0
: DEFAULT_TIMEOUT, false);
}
// security check
SecurityManager sm = System.getSecurityManager();
if (!isConnected() && null != sm) {
try {
sm.checkAccept(receivePacket.getAddress().getHostAddress(),
receivePacket.getPort());
} catch (SecurityException e) {
// do discard the datagram packet
receivePacket = null;
}
}
if (null != receivePacket && null != receivePacket.getAddress()) {
// copy the data of received packet
if (received > 0) {
target.position(oldposition + received);
}
retAddr = receivePacket.getSocketAddress();
break;
}
} while (loop);
return retAddr;
}
示例14: readImpl
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
private int readImpl(ByteBuffer readBuffer) throws IOException {
synchronized (readLock) {
int readCount = 0;
try {
begin();
// timeout == 0 means block read.
// DEFAULT_TIMEOUT is used in non-block mode.
int timeout = isBlocking() ? 0 : DEFAULT_TIMEOUT;
int start = readBuffer.position();
int length = readBuffer.remaining();
if (readBuffer.isDirect()) {
long address = AddressUtil
.getDirectBufferAddress(readBuffer);
if (isConnected()) {
readCount = networkSystem.recvConnectedDatagramDirect(
fd, null, address, start, length, timeout,
false);
} else {
readCount = networkSystem.receiveDatagramDirect(fd,
null, address, start, length, timeout, false);
}
} else {
// the target is assured to have array.
byte[] target = readBuffer.array();
start += readBuffer.arrayOffset();
if (isConnected()) {
readCount = networkSystem.recvConnectedDatagram(fd,
null, target, start, length, timeout, false);
} else {
readCount = networkSystem.receiveDatagram(fd, null,
target, start, length, timeout, false);
}
}
return readCount;
} catch (InterruptedIOException e) {
// InterruptedIOException will be thrown when timeout.
return 0;
} finally {
end(readCount > 0);
}
}
}
示例15: test_getDirectBufferAddress
import org.apache.harmony.nio.AddressUtil; //导入依赖的package包/类
/**
* @tests AddressUtil#getDirectBufferAddress
*/
public void test_getDirectBufferAddress() throws Exception {
ByteBuffer buf = ByteBuffer.allocateDirect(10);
assertTrue(AddressUtil.getDirectBufferAddress(buf) != 0);
}