diff src/ch/ethz/ssh2/StreamGobbler.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
line wrap: on
line diff
--- a/src/ch/ethz/ssh2/StreamGobbler.java	Wed Jul 30 12:09:51 2014 -0700
+++ b/src/ch/ethz/ssh2/StreamGobbler.java	Wed Jul 30 14:16:58 2014 -0700
@@ -36,201 +36,169 @@
  * @version 2.50, 03/15/10
  */
 
-public class StreamGobbler extends InputStream
-{
-	class GobblerThread extends Thread
-	{
-		@Override
-		public void run()
-		{
-			byte[] buff = new byte[8192];
+public class StreamGobbler extends InputStream {
+    class GobblerThread extends Thread {
+        @Override
+        public void run() {
+            byte[] buff = new byte[8192];
 
-			while (true)
-			{
-				try
-				{
-					int avail = is.read(buff);
+            while (true) {
+                try {
+                    int avail = is.read(buff);
 
-					synchronized (synchronizer)
-					{
-						if (avail <= 0)
-						{
-							isEOF = true;
-							synchronizer.notifyAll();
-							break;
-						}
+                    synchronized (synchronizer) {
+                        if (avail <= 0) {
+                            isEOF = true;
+                            synchronizer.notifyAll();
+                            break;
+                        }
 
-						int space_available = buffer.length - write_pos;
+                        int space_available = buffer.length - write_pos;
 
-						if (space_available < avail)
-						{
-							/* compact/resize buffer */
-
-							int unread_size = write_pos - read_pos;
-							int need_space = unread_size + avail;
-
-							byte[] new_buffer = buffer;
+                        if (space_available < avail) {
+                            /* compact/resize buffer */
+                            int unread_size = write_pos - read_pos;
+                            int need_space = unread_size + avail;
+                            byte[] new_buffer = buffer;
 
-							if (need_space > buffer.length)
-							{
-								int inc = need_space / 3;
-								inc = (inc < 256) ? 256 : inc;
-								inc = (inc > 8192) ? 8192 : inc;
-								new_buffer = new byte[need_space + inc];
-							}
+                            if (need_space > buffer.length) {
+                                int inc = need_space / 3;
+                                inc = (inc < 256) ? 256 : inc;
+                                inc = (inc > 8192) ? 8192 : inc;
+                                new_buffer = new byte[need_space + inc];
+                            }
 
-							if (unread_size > 0)
-								System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
-
-							buffer = new_buffer;
+                            if (unread_size > 0)
+                                System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
 
-							read_pos = 0;
-							write_pos = unread_size;
-						}
-
-						System.arraycopy(buff, 0, buffer, write_pos, avail);
-						write_pos += avail;
+                            buffer = new_buffer;
+                            read_pos = 0;
+                            write_pos = unread_size;
+                        }
 
-						synchronizer.notifyAll();
-					}
-				}
-				catch (IOException e)
-				{
-					synchronized (synchronizer)
-					{
-						exception = e;
-						synchronizer.notifyAll();
-						break;
-					}
-				}
-			}
-		}
-	}
+                        System.arraycopy(buff, 0, buffer, write_pos, avail);
+                        write_pos += avail;
+                        synchronizer.notifyAll();
+                    }
+                }
+                catch (IOException e) {
+                    synchronized (synchronizer) {
+                        exception = e;
+                        synchronizer.notifyAll();
+                        break;
+                    }
+                }
+            }
+        }
+    }
 
-	private InputStream is;
+    private InputStream is;
 
-	private final Object synchronizer = new Object();
+    private final Object synchronizer = new Object();
 
-	private boolean isEOF = false;
-	private boolean isClosed = false;
-	private IOException exception = null;
-
-	private byte[] buffer = new byte[2048];
-	private int read_pos = 0;
-	private int write_pos = 0;
+    private boolean isEOF = false;
+    private boolean isClosed = false;
+    private IOException exception = null;
 
-	public StreamGobbler(InputStream is)
-	{
-		this.is = is;
-		GobblerThread t = new GobblerThread();
-		t.setDaemon(true);
-		t.start();
-	}
+    private byte[] buffer = new byte[2048];
+    private int read_pos = 0;
+    private int write_pos = 0;
 
-	@Override
-	public int read() throws IOException
-	{
-        synchronized (synchronizer)
-        {
+    public StreamGobbler(InputStream is) {
+        this.is = is;
+        GobblerThread t = new GobblerThread();
+        t.setDaemon(true);
+        t.start();
+    }
+
+    @Override
+    public int read() throws IOException {
+        synchronized (synchronizer) {
             if (isClosed)
                 throw new IOException("This StreamGobbler is closed.");
 
-            while (read_pos == write_pos)
-            {
+            while (read_pos == write_pos) {
                 if (exception != null)
                     throw exception;
 
                 if (isEOF)
                     return -1;
 
-                try
-                {
+                try {
                     synchronizer.wait();
                 }
-                catch (InterruptedException e)
-                {
+                catch (InterruptedException e) {
                     throw new InterruptedIOException();
                 }
             }
+
             return buffer[read_pos++] & 0xff;
         }
     }
 
     @Override
-	public int available() throws IOException
-	{
-		synchronized (synchronizer)
-		{
-			if (isClosed)
-				throw new IOException("This StreamGobbler is closed.");
+    public int available() throws IOException {
+        synchronized (synchronizer) {
+            if (isClosed)
+                throw new IOException("This StreamGobbler is closed.");
 
-			return write_pos - read_pos;
-		}
-	}
+            return write_pos - read_pos;
+        }
+    }
 
-	@Override
-	public int read(byte[] b) throws IOException
-	{
-		return read(b, 0, b.length);
-	}
+    @Override
+    public int read(byte[] b) throws IOException {
+        return read(b, 0, b.length);
+    }
 
-	@Override
-	public void close() throws IOException
-	{
-		synchronized (synchronizer)
-		{
-			if (isClosed)
-				return;
-			isClosed = true;
-			isEOF = true;
-			synchronizer.notifyAll();
-			is.close();
-		}
-	}
+    @Override
+    public void close() throws IOException {
+        synchronized (synchronizer) {
+            if (isClosed)
+                return;
 
-	@Override
-	public int read(byte[] b, int off, int len) throws IOException
-	{
-		if (b == null)
-			throw new NullPointerException();
+            isClosed = true;
+            isEOF = true;
+            synchronizer.notifyAll();
+            is.close();
+        }
+    }
 
-		if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
-			throw new IndexOutOfBoundsException();
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (b == null)
+            throw new NullPointerException();
+
+        if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
+            throw new IndexOutOfBoundsException();
 
         if (len == 0)
             return 0;
-        synchronized (synchronizer)
-        {
+
+        synchronized (synchronizer) {
             if (isClosed)
                 throw new IOException("This StreamGobbler is closed.");
 
-            while (read_pos == write_pos)
-            {
+            while (read_pos == write_pos) {
                 if (exception != null)
                     throw exception;
 
                 if (isEOF)
                     return -1;
 
-                try
-                {
+                try {
                     synchronizer.wait();
                 }
-                catch (InterruptedException e)
-                {
+                catch (InterruptedException e) {
                     throw new InterruptedIOException();
                 }
             }
 
             int avail = write_pos - read_pos;
-
             avail = (avail > len) ? len : avail;
-
             System.arraycopy(buffer, read_pos, b, off, avail);
-
             read_pos += avail;
-
             return avail;
         }
-	}
+    }
 }