本文整理汇总了Java中java.io.DataOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java DataOutputStream.close方法的具体用法?Java DataOutputStream.close怎么用?Java DataOutputStream.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.DataOutputStream
的用法示例。
在下文中一共展示了DataOutputStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSerialize
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Assert that MKeyBase can be serialized and de-serialized as expected.
*
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void testSerialize() throws IOException, ClassNotFoundException {
final MKeyBase key = new MKeyBase("abc".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
key.toData(dos);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
final MKeyBase readKey = new MKeyBase();
readKey.fromData(dis);
dis.close();
dos.close();
bos.close();
/** assert that two objects are different but are same for equals and compareTo **/
assertFalse(key == readKey);
assertEquals(key, readKey);
assertTrue(key.equals(readKey));
assertEquals(key.hashCode(), readKey.hashCode());
assertEquals(0, key.compareTo(key));
}
示例2: addBodyIfExists
import java.io.DataOutputStream; //导入方法依赖的package包/类
private boolean addBodyIfExists(StethoURLConnectionManager connectionManager, HttpURLConnection connection, Request<?> request)
throws IOException, AuthFailureError {
byte[] body = request.getBody();
if (body != null) {
connection.setDoOutput(true);
connection.addRequestProperty("Content-Type", request.getBodyContentType());
connectionManager.preConnect(connection, formRequestEntity(body));
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();
return true;
}
return false;
}
示例3: sendData
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Sends the data to the bStats server.
*
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JSONObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null!");
}
if (Bukkit.isPrimaryThread()) {
throw new IllegalAccessException("This method must not be called from the main thread!");
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}
示例4: storeRMDTMasterKeyState
import java.io.DataOutputStream; //导入方法依赖的package包/类
@Override
protected void storeRMDTMasterKeyState(DelegationKey masterKey)
throws IOException {
String dbKey = getRMDTMasterKeyNodeKey(masterKey);
if (LOG.isDebugEnabled()) {
LOG.debug("Storing token master key to " + dbKey);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(os);
try {
masterKey.write(out);
} finally {
out.close();
}
try {
db.put(bytes(dbKey), os.toByteArray());
} catch (DBException e) {
throw new IOException(e);
}
}
示例5: setSessionLock
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Creates a session lock file for this process
*/
private void setSessionLock()
{
try
{
File file1 = new File(this.worldDirectory, "session.lock");
DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
try
{
dataoutputstream.writeLong(this.initializationTime);
}
finally
{
dataoutputstream.close();
}
}
catch (IOException ioexception)
{
ioexception.printStackTrace();
throw new RuntimeException("Failed to check session lock, aborting");
}
}
示例6: saveDat
import java.io.DataOutputStream; //导入方法依赖的package包/类
static boolean saveDat(String path, AhoCorasickDoubleArrayTrie<Pinyin[]> trie, Set<Map.Entry<String, Pinyin[]>> entrySet)
{
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + Predefine.BIN_EXT));
out.writeInt(entrySet.size());
for (Map.Entry<String, Pinyin[]> entry : entrySet)
{
Pinyin[] value = entry.getValue();
out.writeInt(value.length);
for (Pinyin pinyin : value)
{
out.writeInt(pinyin.ordinal());
}
}
trie.save(out);
out.close();
}
catch (Exception e)
{
logger.warning("缓存值dat" + path + "失败");
return false;
}
return true;
}
示例7: getUTF8Bytes
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* <p>
* Get UTF8 bytes from a string
* </p>
*
* @param string
* String
* @return UTF8 byte array, or null if failed to get UTF8 byte array
*/
public static byte[] getUTF8Bytes(String string) {
if (string == null)
return new byte[0];
try {
return string.getBytes(ENCODING_UTF8);
} catch (UnsupportedEncodingException e) {
/*
* If system doesn't support UTF-8, use another way
*/
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(string);
byte[] jdata = bos.toByteArray();
bos.close();
dos.close();
byte[] buff = new byte[jdata.length - 2];
System.arraycopy(jdata, 2, buff, 0, buff.length);
return buff;
} catch (IOException ex) {
return new byte[0];
}
}
}
示例8: main
import java.io.DataOutputStream; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(1);
dos.writeInt(0x7fffffff);
dos.writeInt(0x8fffffff);
dos.writeInt(0xffffffff);
dos.close();
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
for (int i=0; i<4; i++) {
long res = iis.readUnsignedInt();
if (res <= 0) {
throw new RuntimeException("Negative number was read: "+
Long.toString(res, 16));
}
}
}
示例9: createFile
import java.io.DataOutputStream; //导入方法依赖的package包/类
private static File createFile( String name, byte[] data ) throws IOException
{
File jpeg = File.createTempFile( name, "jpg" );
jpeg.createNewFile();
DataOutputStream os = new DataOutputStream( new FileOutputStream( jpeg ) );
os.write( data );
os.close();
// This file will be deleted when the JVM
// will exit.
jpeg.deleteOnExit();
return jpeg;
}
示例10: sendData
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Sends the data to the bStats server.
*
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JsonObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null");
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}
示例11: writePrepWithUnkownLength
import java.io.DataOutputStream; //导入方法依赖的package包/类
private int writePrepWithUnkownLength(Writer writer, int start, int n)
throws IOException {
for (int i = start; i < (start + n); i++) {
DataOutputStream out = writer.prepareAppendKey(-1);
String localKey = String.format(localFormatter, i);
out.write(localKey.getBytes());
out.close();
String value = "value" + localKey;
out = writer.prepareAppendValue(-1);
out.write(value.getBytes());
out.close();
}
return (start + n);
}
示例12: run
import java.io.DataOutputStream; //导入方法依赖的package包/类
@Override
public void run() {
try {
String sb = csebase.getServiceUrl() +"/" + ServiceAEName + "/" + container_name;
URL mUrl = new URL(sb);
HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Accept", "application/xml");
conn.setRequestProperty("Content-Type", "application/vnd.onem2m-res+xml;ty=4");
conn.setRequestProperty("locale", "ko");
conn.setRequestProperty("X-M2M-RI", "12345");
conn.setRequestProperty("X-M2M-Origin", ae.getAEid() );
String reqContent = contentinstance.makeXML();
conn.setRequestProperty("Content-Length", String.valueOf(reqContent.length()));
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(reqContent.getBytes());
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String resp = "";
String strLine="";
while ((strLine = in.readLine()) != null) {
resp += strLine;
}
if (resp != "") {
receiver.getResponseBody(resp);
}
conn.disconnect();
} catch (Exception exp) {
LOG.log(Level.SEVERE, exp.getMessage());
}
}
示例13: writeJsonAsBytes
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Write the json as bytes -then close the file
* @param dataOutputStream an outout stream that will always be closed
* @throws IOException on any failure
*/
private void writeJsonAsBytes(T instance,
DataOutputStream dataOutputStream) throws IOException {
try {
byte[] b = toBytes(instance);
dataOutputStream.write(b);
} finally {
dataOutputStream.close();
}
}
示例14: openConnection
import java.io.DataOutputStream; //导入方法依赖的package包/类
/**
* Opens an {@link HttpURLConnection} with parameters.
*
* @param request
* @param listener
* @return an open connection
* @throws IOException
*/
private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException {
URL url = new URL(request.url);
HttpURLConnection connection = createConnection(url);
connection.setConnectTimeout(request.timeoutMs);
connection.setReadTimeout(request.timeoutMs);
connection.setUseCaches(false);
connection.setDoInput(true);
if (request.paramMap != null) {
Set<String> keySets = request.paramMap.keySet();
for (String key : keySets) {
connection.addRequestProperty(key, request.paramMap.get(key));
}
}
if ("POST".equals(request.method) || "PUT".equals(request.method) || "PATCH".equals(request.method)) {
connection.setRequestMethod(request.method);
if (request.body != null) {
if (listener != null) {
listener.onHttpUploadProgress(0);
}
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//TODO big stream will cause OOM; Progress callback is meaningless
out.write(request.body.getBytes());
out.close();
if (listener != null) {
listener.onHttpUploadProgress(100);
}
}
} else if (!TextUtils.isEmpty(request.method)) {
connection.setRequestMethod(request.method);
} else {
connection.setRequestMethod("GET");
}
return connection;
}
示例15: write
import java.io.DataOutputStream; //导入方法依赖的package包/类
public static void write(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException
{
DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_));
try
{
write(p_74795_0_, dataoutputstream);
}
finally
{
dataoutputstream.close();
}
}