package com.fishercoder.solutions;

/**
 * The API: int read4(char *buf) reads 4 characters at a time from a file.

 The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.

 By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

 Note:
 The read function may be called multiple times.
 */
public class _158 {

	/**
	 * @param buf Destination buffer
	 * @param n   Maximum number of characters to read
	 * @return    The number of characters read
	 */
	private int buffPtr = 0;
	private int buffCnt = 0;
	private char[] buff = new char[4];

	public int read(char[] buf, int n) {
		int ptr = 0;
		while (ptr < n) {
			if (buffPtr == 0) {
				buffCnt = read4(buff);
			}
			if (buffCnt == 0) {
				break;
			}
			while (ptr < n && buffPtr < buffCnt) {
				buf[ptr++] = buff[buffPtr++];
			}
			if (buffPtr >= buffCnt) {
				buffPtr = 0;
			}
		}
		return ptr;
	}

	//This is a fake method to make IDE happy.
	private int read4(char[] buff) {
		return 1;
	}

}

@@ -34,17 +34,76 @@
 ;%0A%0A/**%0A 
-*
+158. Read N Characters Given Read4 II - Call multiple times%0A
  The API
@@ -477,39 +477,304 @@
 es.%0A
- */%0Apublic class _158 %7B%0A%0A%09/**%0A%09
+%0A Example 1:%0A%0A Given buf = %22abc%22%0A read(%22abc%22, 1) // returns %22a%22%0A read(%22abc%22, 2); // returns %22bc%22%0A read(%22abc%22, 1); // returns %22%22%0A%0A Example 2:%0A%0A Given buf = %22abc%22%0A read(%22abc%22, 4) // returns %22abc%22%0A read(%22abc%22, 1); // returns %22%22%0A */%0Apublic class _158 %7B%0A%0A  public static class Solution1 %7B%0A    /**%0A    
  * @
@@ -802,17 +802,20 @@
  buffer%0A
-%09
+    
  * @para
@@ -857,17 +857,20 @@
 to read%0A
-%09
+    
  * @retu
@@ -872,19 +872,16 @@
 @return 
-   
 The numb
@@ -906,14 +906,20 @@
 ead%0A
-%09 */%0A%09
+     */%0A    
 priv
@@ -935,25 +935,28 @@
 uffPtr = 0;%0A
-%09
+    
 private int 
@@ -968,17 +968,20 @@
 nt = 0;%0A
-%09
+    
 private 
@@ -1008,17 +1008,20 @@
 ar%5B4%5D;%0A%0A
-%09
+    
 public i
@@ -1049,18 +1049,22 @@
 nt n) %7B%0A
-%09%09
+      
 int ptr 
@@ -1068,18 +1068,22 @@
 tr = 0;%0A
-%09%09
+      
 while (p
@@ -1092,19 +1092,24 @@
  %3C n) %7B%0A
-%09%09%09
+        
 if (buff
@@ -1116,28 +1116,34 @@
 Ptr == 0) %7B%0A
-%09%09%09%09
+          
 buffCnt = re
@@ -1153,24 +1153,34 @@
 (buff);%0A
-%09%09%09%7D%0A%09%09%09
+        %7D%0A        
 if (buff
@@ -1195,27 +1195,43 @@
 ) %7B%0A
-%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%09
+          break;%0A        %7D%0A        
 whil
@@ -1265,20 +1265,26 @@
 fCnt) %7B%0A
-%09%09%09%09
+          
 buf%5Bptr+
@@ -1309,16 +1309,26 @@
 +%5D;%0A
-%09%09%09%7D%0A%09%09%09
+        %7D%0A        
 if (
@@ -1353,12 +1353,18 @@
 ) %7B%0A
-%09%09%09%09
+          
 buff
@@ -1376,19 +1376,32 @@
  0;%0A
-%09%09%09%7D%0A%09%09%7D%0A%09%09
+        %7D%0A      %7D%0A      
 retu
@@ -1412,13 +1412,19 @@
 tr;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 //Th
@@ -1462,17 +1462,20 @@
  happy.%0A
-%09
+    
 private 
@@ -1503,10 +1503,14 @@
 ) %7B%0A
-%09%09
+      
 retu
@@ -1519,10 +1519,16 @@
  1;%0A
-%09%7D%0A
+    %7D%0A  %7D
 %0A%7D%0A

refactor 158