/* Copyright (C) 2014 KKBOX Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * ​http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kkbox.toolkit.api;

import android.content.Context;

import com.kkbox.toolkit.internal.api.APIRequest;
import com.kkbox.toolkit.internal.api.KKAPIJsonRequestListener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

public class KKAPIJsonRequest extends APIRequest {

	private KKAPIJsonRequestListener jsonRequestListener;

	public KKAPIJsonRequest(String url, Cipher cipher, long reloadPeriod, Context context) {
		super(url, cipher, reloadPeriod, context);
	}

	public KKAPIJsonRequest(String url, Cipher cipher) {
		super(url, cipher);
	}

	public KKAPIJsonRequest(String url, Cipher cipher, int socketTimeout) {
		super(url, cipher, socketTimeout);
	}

	@Override
	public Void doInBackground(Object... params) {
		jsonRequestListener = (KKAPIJsonRequestListener) params[0];
		return super.doInBackground(params);
	}

	@Override
	protected void readDataFromInputStream(ByteArrayOutputStream data) throws IOException {}

	@Override
	protected void preCompleteAndCachedAPI(ByteArrayOutputStream data, File cacheFile) throws BadPaddingException, IllegalBlockSizeException, IOException {
		jsonRequestListener.onStreamPreComplete(is);
		if (cacheTimeOut > 0) {
			try {
				int readLength;
				byte[] buffer = new byte[128];
				while ((readLength = is.read(buffer, 0, buffer.length)) != -1) {
					data.write(buffer, 0, readLength);
				}
				data.flush();
				FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
				OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
				outputStreamWriter.write(data.toString());
				outputStreamWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

@@ -824,16 +824,48 @@
 o.File;%0A
+import java.io.FileInputStream;%0A
 import j
@@ -909,32 +909,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.io.O
@@ -1975,51 +1975,31 @@
 %7B%0A%09%09
-jsonRequestListener.onStreamPreComplete(is)
+InputStream inputStream
 ;%0A%09%09
@@ -2026,18 +2026,8 @@
 ) %7B%0A
-%09%09%09try %7B%0A%09
 %09%09%09i
@@ -2041,17 +2041,16 @@
 Length;%0A
-%09
 %09%09%09byte%5B
@@ -2076,16 +2076,87 @@
 %5B128%5D;%0A%09
+%09%09FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);%0A
 %09%09%09while
@@ -2219,21 +2219,32 @@
 ) %7B%0A%09%09%09%09
-%09data
+fileOutputStream
 .write(b
@@ -2273,57 +2273,44 @@
 %0A%09%09%09
-%09
 %7D%0A%09%09%09
-%09data.flush();%0A%09%09%09%09FileOutputStream fileOut
+fileOutputStream.close();%0A%09%09%09in
 putS
@@ -2317,35 +2317,34 @@
 tream = new File
-Out
+In
 putStream(cacheF
@@ -2355,233 +2355,97 @@
 ;%0A%09%09
-%09%09OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);%0A%09%09%09%09outputStreamWriter.write(data.toString());%0A%09%09%09%09outputStreamWriter.close();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D
+%7D else %7B%0A%09%09%09inputStream = is;%0A%09%09%7D%0A%09%09jsonRequestListener.onStreamPreComplete(inputStream);
 %0A%09%7D%0A

Fix the issue that input stream has been consumed which can not save to cache file