当前位置: 首页>>代码示例>>Java>>正文


Java WritableByteChannel.close方法代码示例

本文整理汇总了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);
}
 
开发者ID:linkingli,项目名称:FaceDistinguish,代码行数:24,代码来源:HttpUtil.java

示例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();
    }
}
 
开发者ID:istolbov,项目名称:i_stolbov,代码行数:23,代码来源:CommandFactoryServer.java

示例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.");
    }
}
 
开发者ID:istolbov,项目名称:i_stolbov,代码行数:28,代码来源:CommandFactoryClient.java

示例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());
}
 
开发者ID:begeekmyfriend,项目名称:mp4parser_android,代码行数:14,代码来源:SampleEntry.java

示例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);
}
 
开发者ID:Nexenta,项目名称:edgex-java-connector,代码行数:73,代码来源:EdgexClient.java

示例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);
}
 
开发者ID:Nexenta,项目名称:edgex-java-connector,代码行数:69,代码来源:EdgexClient.java

示例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);
}
 
开发者ID:Nexenta,项目名称:edgex-java-connector,代码行数:71,代码来源:EdgexClient.java


注:本文中的java.nio.channels.WritableByteChannel.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。