本文整理汇总了Java中java.nio.channels.WritableByteChannel.close方法的典型用法代码示例。如果您正苦于以下问题:Java WritableByteChannel.close方法的具体用法?Java WritableByteChannel.close怎么用?Java WritableByteChannel.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.WritableByteChannel
的用法示例。
在下文中一共展示了WritableByteChannel.close方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readStreamAsStr
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* 将流转换为字符串
*
* @param is
* @return
* @throws IOException
*/
public static String readStreamAsStr(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
WritableByteChannel dest = Channels.newChannel(bos);
ReadableByteChannel src = Channels.newChannel(is);
ByteBuffer bb = ByteBuffer.allocate(4096);
while (src.read(bb) != -1) {
bb.flip();
dest.write(bb);
bb.clear();
}
src.close();
dest.close();
return new String(bos.toByteArray(), Constants.ENCODING);
}
示例2: execute
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* execute.
* @param cmd - cmd
* @throws IOException - IOException
*/
@Override
public void execute(Command cmd) throws IOException {
Path filePath = workPath.resolve(cmd.getParam()).normalize();
if (Files.exists(filePath, LinkOption.NOFOLLOW_LINKS)
&& !Files.isDirectory(filePath) && Files.isReadable(filePath)) {
dataOutputStream.writeUTF(SUCCESS);
dataOutputStream.flush();
WritableByteChannel wbc = Channels.newChannel(dataOutputStream);
FileInputStream fis = new FileInputStream(filePath.toString());
fis.getChannel().transferTo(0, Long.MAX_VALUE, wbc);
wbc.close();
} else {
dataOutputStream.writeUTF(ERROR);
dataOutputStream.flush();
}
}
示例3: execute
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* execute.
* @param cmd - cmd
* @throws IOException - IOException
*/
@Override
public void execute(Command cmd) throws IOException {
Path filePath = Paths.get(cmd.getParam());
if (Files.exists(filePath, LinkOption.NOFOLLOW_LINKS)
&& !Files.isDirectory(filePath) && Files.isReadable(filePath)) {
System.out.println("Uploading...");
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject(cmd);
oos.flush();
WritableByteChannel rbc = Channels.newChannel(new DataOutputStream(outputStream));
FileInputStream fis = new FileInputStream(cmd.getParam());
fis.getChannel().transferTo(0, Long.MAX_VALUE, rbc);
rbc.close();
System.out.println("Done.");
} else {
System.out.println("Error. Please try again.");
}
}
示例4: _writeChildBoxes
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
public void _writeChildBoxes(ByteBuffer bb) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableByteChannel wbc = Channels.newChannel(baos);
try {
for (Box box : boxes) {
box.getBox(wbc);
}
wbc.close();
} catch (IOException e) {
throw new RuntimeException("Cannot happen. Everything should be in memory and therefore no exceptions.");
}
bb.put(baos.toByteArray());
}
示例5: write
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* Write data to the object
*
* @param bucket - bucket name
* @param object - object name
* @param arr - data buffer
* @param off - offset
* @param more - true to continue stream operations, false to finish stream operations
* @return error code
*/
public int write(String bucket, String object, ByteBuffer arr[], long off, boolean more) {
int mode = (more ? SS_CONT : SS_FIN);
long totlen = 0;
int i;
for (i = 0; i < arr.length; i++) {
arr[i].flip();
totlen += arr[i].limit();
}
if (this.debugMode > 0) {
debug("\n");
debug("write");
debug("write bucket", bucket);
debug("write object", object);
debug("write off", off);
debug("write totlen", totlen);
debug("write more", more);
debug("write sid", sid);
}
String method = "POST";
this.path = '/' + bucket + '/' + object;
this.path += "?comp=streamsession";
this.path += (mode == SS_FIN ? "&finalize" : "");
// init connection
err = init(method, totlen > 0);
if (err != 0) {
return err;
}
// add request headers
if (sid != null)
setRequestHeader("x-session-id", sid);
setRequestHeader("x-ccow-offset", off + "");
setRequestHeader("x-ccow-length", totlen + "");
setRequestHeader("Content-Length", totlen + "");
this.setRequestHeader("Content-Type", "application/octet-stream");
WritableByteChannel channel = null;
if (!this.connect(method, 82, "IO error url: " + this.url))
return err;
try {
// Send data request
if (totlen > 0) {
OutputStream wr = con.getOutputStream();
channel = Channels.newChannel(wr);
for (i = 0; i < arr.length; i++) {
channel.write(arr[i]);
}
channel.close();
wr.close();
}
} catch (IOException e) {
createErrorMessage(82, "IO error url: " + this.url, e);
return err;
}
return read(mode, false);
}
示例6: append
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* Append to the object
* @param bucket - bucket name
* @param object - object name
* @param arr - data buffers
* @return error code
*/
public int append(String bucket, String object, ByteBuffer arr[]) {
int mode = SS_FIN;
long totlen = 0;
int i;
for (i = 0; i < arr.length; i++) {
arr[i].flip();
totlen += arr[i].limit();
}
if (this.debugMode > 0) {
debug("\n");
debug("append");
debug("append bucket:", bucket);
debug("append object:", object);
debug("append sid:", sid);
debug("append totlen:", totlen);
}
String method = "PUT";
this.path = '/' + bucket + '/' + object;
this.path += "?comp=appendblock";
this.path += (mode == SS_FIN ? "&finalize" : "");
// init connection
err = init(method, totlen > 0);
if (err != 0) {
return err;
}
// add request headers
if (sid != null)
setRequestHeader("x-session-id", sid);
setRequestHeader("x-ccow-length", totlen + "");
setRequestHeader("Content-Length", totlen + "");
WritableByteChannel channel = null;
if (!this.connect(method, 83, "IO error url: " + this.url))
return err;
try {
// Send data request
if (totlen > 0) {
OutputStream wr = con.getOutputStream();
channel = Channels.newChannel(wr);
for (i = 0; i < arr.length; i++) {
channel.write(arr[i]);
}
channel.close();
wr.close();
}
} catch (IOException e) {
createErrorMessage(83, "IO error url: " + this.url, e);
return err;
}
return read(mode, false);
}
示例7: writeBlock
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* Update block inside the object
* @param bucket - bucket name
* @param object - object name
* @param arr - data buffers
* @param off - block offset
* @return error code
*/
public int writeBlock(String bucket, String object, ByteBuffer arr[], long off) {
int mode = SS_FIN;
long totlen = 0;
int i;
for (i = 0; i < arr.length; i++) {
arr[i].flip();
totlen += arr[i].limit();
}
if (this.debugMode > 0) {
debug("\n");
debug("writeBlock");
debug("writeBlock bucket:", bucket);
debug("writeBlock object:", object);
debug("writeBlock off:", off);
debug("writeBlock sid:", sid);
debug("writeBlock totlen:", totlen);
}
String method = "PUT";
this.path = '/' + bucket + '/' + object;
this.path += "?comp=randwrblock";
this.path += (mode == SS_FIN ? "&finalize" : "");
// init connection
err = init(method, totlen > 0);
if (err != 0) {
return err;
}
// add request headers
if (sid != null)
setRequestHeader("x-session-id", sid);
setRequestHeader("x-ccow-offset", off + "");
setRequestHeader("x-ccow-length", totlen + "");
setRequestHeader("Content-Length", totlen + "");
WritableByteChannel channel = null;
if (!this.connect(method, 84, "IO error url: " + this.url))
return err;
try {
// Send data request
if (totlen > 0) {
OutputStream wr = con.getOutputStream();
channel = Channels.newChannel(wr);
for (i = 0; i < arr.length; i++) {
channel.write(arr[i]);
}
channel.close();
wr.close();
}
} catch (IOException e) {
createErrorMessage(84, "IO error url: " + this.url, e);
return err;
}
return read(mode, false);
}