directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [3/4] directory-kerby git commit: Switching FileOutputStream to Files.newOutputStream
Date Wed, 26 Apr 2017 10:36:09 GMT
Switching FileOutputStream to Files.newOutputStream


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/f0d593fc
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/f0d593fc
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/f0d593fc

Branch: refs/heads/trunk
Commit: f0d593fc772163b41defeea0f983f4bfc8e8c4c7
Parents: 85d4771
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Wed Apr 26 11:13:59 2017 +0100
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Wed Apr 26 11:13:59 2017 +0100

----------------------------------------------------------------------
 .../java/org/apache/kerby/asn1/util/IOUtil.java | 30 +++++++++++---------
 .../main/java/org/apache/kerby/util/IOUtil.java | 30 +++++++++++---------
 .../java/org/apache/kerby/xdr/util/IOUtil.java  | 30 +++++++++++---------
 .../kerb/crypto/random/NativeRandom.java        |  5 ++--
 .../kerberos/kerb/ccache/CredentialCache.java   |  7 ++---
 .../kerby/kerberos/kerb/keytab/Keytab.java      |  7 ++---
 6 files changed, 57 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java
b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java
index 02d1531..aa9323b 100644
--- a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java
+++ b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/util/IOUtil.java
@@ -21,12 +21,13 @@ package org.apache.kerby.asn1.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
 
 /**
  * Some IO and file related utilities.
@@ -35,14 +36,15 @@ public final class IOUtil {
     private IOUtil() { }
 
     public static byte[] readInputStream(InputStream in) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[1024];
-        int length = 0;
-        while ((length = in.read(buffer)) != -1) {
-            baos.write(buffer, 0, length);
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            byte[] buffer = new byte[1024];
+            int length = 0;
+            while ((length = in.read(buffer)) != -1) {
+                baos.write(buffer, 0, length);
+            }
+            in.close();
+            return baos.toByteArray();
         }
-        in.close();
-        return baos.toByteArray();
     }
 
     public static void readInputStream(InputStream in,
@@ -86,7 +88,7 @@ public final class IOUtil {
         }
         byte[] buf = new byte[(int) len];
 
-        InputStream is = new FileInputStream(file);
+        InputStream is = Files.newInputStream(file.toPath());
         readInputStream(is, buf);
 
         return Utf8.toString(buf);
@@ -99,11 +101,11 @@ public final class IOUtil {
      * @throws IOException e
      */
     public static void writeFile(String content, File file) throws IOException {
-        FileOutputStream outputStream = new FileOutputStream(file);
-        FileChannel fc = outputStream.getChannel();
+        OutputStream outputStream = Files.newOutputStream(file.toPath());
+        WritableByteChannel channel = Channels.newChannel(outputStream);
 
         ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content));
-        fc.write(buffer);
+        channel.write(buffer);
         outputStream.close();
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-common/kerby-util/src/main/java/org/apache/kerby/util/IOUtil.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-util/src/main/java/org/apache/kerby/util/IOUtil.java b/kerby-common/kerby-util/src/main/java/org/apache/kerby/util/IOUtil.java
index 122f211..a741531 100644
--- a/kerby-common/kerby-util/src/main/java/org/apache/kerby/util/IOUtil.java
+++ b/kerby-common/kerby-util/src/main/java/org/apache/kerby/util/IOUtil.java
@@ -21,12 +21,13 @@ package org.apache.kerby.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+import java.nio.file.Files;
 
 /**
  * Some IO and file related utilities.
@@ -35,14 +36,15 @@ public final class IOUtil {
     private IOUtil() { }
 
     public static byte[] readInputStream(InputStream in) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[1024];
-        int length = 0;
-        while ((length = in.read(buffer)) != -1) {
-            baos.write(buffer, 0, length);
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            byte[] buffer = new byte[1024];
+            int length = 0;
+            while ((length = in.read(buffer)) != -1) {
+                baos.write(buffer, 0, length);
+            }
+            in.close();
+            return baos.toByteArray();
         }
-        in.close();
-        return baos.toByteArray();
     }
 
     public static void readInputStream(InputStream in,
@@ -87,7 +89,7 @@ public final class IOUtil {
         }
         byte[] buf = new byte[(int) len];
 
-        InputStream is = new FileInputStream(file);
+        InputStream is = Files.newInputStream(file.toPath());
         readInputStream(is, buf);
 
         return Utf8.toString(buf);
@@ -100,11 +102,11 @@ public final class IOUtil {
      * @throws IOException e
      */
     public static void writeFile(String content, File file) throws IOException {
-        FileOutputStream outputStream = new FileOutputStream(file);
-        FileChannel fc = outputStream.getChannel();
+        OutputStream outputStream = Files.newOutputStream(file.toPath());
+        WritableByteChannel channel = Channels.newChannel(outputStream);
 
         ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content));
-        fc.write(buffer);
+        channel.write(buffer);
         outputStream.close();
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java
index 2136511..1649269 100644
--- a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java
+++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java
@@ -21,12 +21,13 @@ package org.apache.kerby.xdr.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+import java.nio.file.Files;
 
 /**
  * Some IO and file related utilities.
@@ -35,14 +36,15 @@ public final class IOUtil {
     private IOUtil() { }
 
     public static byte[] readInputStream(InputStream in) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[1024];
-        int length = 0;
-        while ((length = in.read(buffer)) != -1) {
-            baos.write(buffer, 0, length);
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            byte[] buffer = new byte[1024];
+            int length = 0;
+            while ((length = in.read(buffer)) != -1) {
+                baos.write(buffer, 0, length);
+            }
+            in.close();
+            return baos.toByteArray();
         }
-        in.close();
-        return baos.toByteArray();
     }
 
     public static void readInputStream(InputStream in,
@@ -86,7 +88,7 @@ public final class IOUtil {
         }
         byte[] buf = new byte[(int) len];
 
-        InputStream is = new FileInputStream(file);
+        InputStream is = Files.newInputStream(file.toPath());
         readInputStream(is, buf);
 
         return Utf8.toString(buf);
@@ -99,11 +101,11 @@ public final class IOUtil {
      * @throws IOException e
      */
     public static void writeFile(String content, File file) throws IOException {
-        FileOutputStream outputStream = new FileOutputStream(file);
-        FileChannel fc = outputStream.getChannel();
+        OutputStream outputStream = Files.newOutputStream(file.toPath());
+        WritableByteChannel channel = Channels.newChannel(outputStream);
 
         ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content));
-        fc.write(buffer);
+        channel.write(buffer);
         outputStream.close();
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/random/NativeRandom.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/random/NativeRandom.java
b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/random/NativeRandom.java
index 4c58a1e..b57bc8c 100644
--- a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/random/NativeRandom.java
+++ b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/random/NativeRandom.java
@@ -22,10 +22,11 @@ package org.apache.kerby.kerberos.kerb.crypto.random;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 
 /**
  * use "/dev/urandom", which is on linux, to implement RandomProvider, so it should be used
on linux.
@@ -47,7 +48,7 @@ public class NativeRandom implements RandomProvider {
     public void setSeed(byte[] seed) {
         OutputStream output = null;
         try {
-            output = new FileOutputStream(randFile);
+            output = Files.newOutputStream(Paths.get(randFile));
             output.write(seed);
             output.flush();
         } catch (IOException e) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/ccache/CredentialCache.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/ccache/CredentialCache.java
b/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/ccache/CredentialCache.java
index d1c8595..0a56626 100644
--- a/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/ccache/CredentialCache.java
+++ b/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/ccache/CredentialCache.java
@@ -24,11 +24,10 @@ import org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket;
 import org.apache.kerby.kerberos.kerb.type.ticket.Ticket;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -81,7 +80,7 @@ public class CredentialCache implements KrbCredentialCache {
 
     @Override
     public void store(File ccacheFile) throws IOException {
-        try (OutputStream outputStream = new FileOutputStream(ccacheFile)) {
+        try (OutputStream outputStream = Files.newOutputStream(ccacheFile.toPath())) {
             store(outputStream);
         }
     }
@@ -190,7 +189,7 @@ public class CredentialCache implements KrbCredentialCache {
                     + ccacheFile.getAbsolutePath());
         }
 
-        try (InputStream inputStream = new FileInputStream(ccacheFile)) {
+        try (InputStream inputStream = Files.newInputStream(ccacheFile.toPath())) {
             load(inputStream);
         }
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f0d593fc/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/keytab/Keytab.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/keytab/Keytab.java
b/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/keytab/Keytab.java
index 460f1aa..52e15d9 100644
--- a/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/keytab/Keytab.java
+++ b/kerby-kerb/kerb-util/src/main/java/org/apache/kerby/kerberos/kerb/keytab/Keytab.java
@@ -24,11 +24,10 @@ import org.apache.kerby.kerberos.kerb.type.base.EncryptionType;
 import org.apache.kerby.kerberos.kerb.type.base.PrincipalName;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -140,7 +139,7 @@ public final class Keytab implements KrbKeytab {
             throw new IllegalArgumentException("Invalid keytab file: " + keytabFile.getAbsolutePath());
         }
 
-        try (InputStream is = new FileInputStream(keytabFile)) {
+        try (InputStream is = Files.newInputStream(keytabFile.toPath())) {
             load(is);
         }
     }
@@ -207,7 +206,7 @@ public final class Keytab implements KrbKeytab {
 
     @Override
     public void store(File keytabFile) throws IOException {
-        try (OutputStream outputStream = new FileOutputStream(keytabFile)) {
+        try (OutputStream outputStream = Files.newOutputStream(keytabFile.toPath())) {
             store(outputStream);
         }
     }


Mime
View raw message