Commit 8a1f7952 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

nfsv41: IO: use ByteBuffer instead of byte[]

parent 67efb646
package org.dcache.chimera.nfs.v4;
import java.nio.ByteBuffer;
import org.dcache.chimera.nfs.v4.xdr.nfsstat4;
import org.dcache.chimera.nfs.v4.xdr.nfs_argop4;
import org.dcache.chimera.nfs.v4.xdr.nfs_opnum4;
......@@ -50,9 +51,9 @@ public class OperationREAD extends AbstractNFSv4Operation {
long offset = _args.opread.offset.value.value;
int count = _args.opread.count.value.value;
byte[] buf = new byte[count];
ByteBuffer buf = ByteBuffer.allocate(count);
int bytesReaded = context.currentInode().read(offset, buf, 0, count);
int bytesReaded = context.currentInode().read(offset, buf.array(), 0, count);
if( bytesReaded < 0 ) {
throw new IOHimeraFsException("IO not allowd");
}
......@@ -60,12 +61,7 @@ public class OperationREAD extends AbstractNFSv4Operation {
res.status = nfsstat4.NFS4_OK;
res.resok4 = new READ4resok();
if (bytesReaded != count) {
res.resok4.data = new byte[bytesReaded];
System.arraycopy(buf, 0, res.resok4.data, 0, bytesReaded);
} else {
res.resok4.data = buf;
}
res.resok4.data = buf;
if( offset + bytesReaded >= inodeStat.getSize() ) {
res.resok4.eof = true;
......
......@@ -34,7 +34,7 @@ public class OperationWRITE extends AbstractNFSv4Operation {
try {
if (_args.opwrite.offset.value.value + _args.opwrite.data.length > 0x3ffffffe){
if (_args.opwrite.offset.value.value + _args.opwrite.data.remaining() > 0x3ffffffe){
throw new ChimeraNFSException(nfsstat4.NFS4ERR_INVAL, "Arbitrary value");
}
......@@ -62,8 +62,8 @@ public class OperationWRITE extends AbstractNFSv4Operation {
}
long offset = _args.opwrite.offset.value.value;
int count = _args.opwrite.data.length;
int bytesWritten = context.currentInode().write(offset, _args.opwrite.data, 0, count);
int count = _args.opwrite.data.remaining();
int bytesWritten = context.currentInode().write(offset, _args.opwrite.data.array(), 0, count);
if( bytesWritten < 0 ) {
throw new IOHimeraFsException("IO not allowed");
......
......@@ -42,11 +42,11 @@ public class DSOperationREAD extends AbstractNFSv4Operation {
long offset = _args.opread.offset.value.value;
int count = _args.opread.count.value.value;
byte[] buf = new byte[count];
ByteBuffer bb = ByteBuffer.allocateDirect(count);
IOReadFile in = new IOReadFile(_poolRoot, context.currentInode().toString(), context.currentInode().stat().getSize());
int bytesReaded = in.read(buf, offset, count);
int bytesReaded = in.read(bb, offset, count);
if( bytesReaded < 0 ) {
eof = true;
bytesReaded = 0;
......@@ -54,8 +54,7 @@ public class DSOperationREAD extends AbstractNFSv4Operation {
res.status = nfsstat4.NFS4_OK;
res.resok4 = new READ4resok();
res.resok4.data = new byte[bytesReaded];
System.arraycopy(buf, 0, res.resok4.data, 0, bytesReaded);
res.resok4.data = bb;
if( offset + bytesReaded == inodeStat.getSize() ) {
eof = true;
......@@ -113,14 +112,9 @@ public class DSOperationREAD extends AbstractNFSv4Operation {
_fc = _in.getChannel();
}
public int read(byte[] b, long off, long len) throws IOException {
ByteBuffer bb = ByteBuffer.wrap(b, 0, (int)len);
public int read(ByteBuffer bb, long off, long len) throws IOException {
bb.rewind();
return _fc.read(bb, off);
}
public void close() throws IOException {
......
......@@ -42,7 +42,7 @@ public class DSOperationWRITE extends AbstractNFSv4Operation {
try {
long offset = _args.opwrite.offset.value.value;
int count = _args.opwrite.data.length;
int count = _args.opwrite.data.remaining();
IOWriteFile out = new IOWriteFile(_poolRoot, context.currentInode().toString(), context.currentInode().stat().getSize() == 0);
......@@ -101,10 +101,9 @@ public class DSOperationWRITE extends AbstractNFSv4Operation {
}
}
public int write(byte[] data, long off, long len) throws IOException {
ByteBuffer bb = ByteBuffer.wrap(data, 0, (int)len);
bb.rewind();
return _fc.write(bb, off);
public int write(ByteBuffer data, long off, long len) throws IOException {
data.rewind();
return _fc.write(data, off);
}
......
......@@ -6,10 +6,11 @@
package org.dcache.chimera.nfs.v4.xdr;
import org.dcache.xdr.*;
import java.io.IOException;
import java.nio.ByteBuffer;
public class READ4resok implements XdrAble {
public boolean eof;
public byte [] data;
public ByteBuffer data;
public READ4resok() {
}
......@@ -22,13 +23,13 @@ public class READ4resok implements XdrAble {
public void xdrEncode(XdrEncodingStream xdr)
throws OncRpcException, IOException {
xdr.xdrEncodeBoolean(eof);
xdr.xdrEncodeDynamicOpaque(data);
xdr.xdrEncodeByteBuffer(data);
}
public void xdrDecode(XdrDecodingStream xdr)
throws OncRpcException, IOException {
eof = xdr.xdrDecodeBoolean();
data = xdr.xdrDecodeDynamicOpaque();
data = xdr.xdrDecodeByteBuffer();
}
}
......
......@@ -6,12 +6,13 @@
package org.dcache.chimera.nfs.v4.xdr;
import org.dcache.xdr.*;
import java.io.IOException;
import java.nio.ByteBuffer;
public class WRITE4args implements XdrAble {
public stateid4 stateid;
public offset4 offset;
public int stable;
public byte[] data;
public ByteBuffer data;
public WRITE4args() {
}
......@@ -26,7 +27,7 @@ public class WRITE4args implements XdrAble {
stateid.xdrEncode(xdr);
offset.xdrEncode(xdr);
xdr.xdrEncodeInt(stable);
xdr.xdrEncodeDynamicOpaque(data);
xdr.xdrEncodeByteBuffer(data);
}
public void xdrDecode(XdrDecodingStream xdr)
......@@ -34,7 +35,7 @@ public class WRITE4args implements XdrAble {
stateid = new stateid4(xdr);
offset = new offset4(xdr);
stable = xdr.xdrDecodeInt();
data = xdr.xdrDecodeDynamicOpaque();
data = xdr.xdrDecodeByteBuffer();
}
}
......
......@@ -330,6 +330,10 @@ public class Xdr implements XdrDecodingStream, XdrEncodingStream {
public void xdrEncodeByteBuffer(ByteBuffer buf) {
buf.limit(buf.position());
buf.rewind();
int len = buf.remaining();
int padding = (4 - (len & 3)) & 3;
xdrEncodeInt(len);
_body.put(buf);
_body.position(_body.position() + padding);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment