/* 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.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 {
		if (is != null) {
			int readLength;
			byte[] buffer = new byte[128];
			FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
			while ((readLength = is.read(buffer, 0, buffer.length)) != -1) {
				fileOutputStream.write(buffer, 0, readLength);
			}
			fileOutputStream.close();
		}
		InputStream inputStream = new FileInputStream(cacheFile);
		jsonRequestListener.onStreamPreComplete(inputStream);
		inputStream.close();
	}
}

@@ -1969,16 +1969,70 @@
 ption %7B%0A
+%09%09InputStream inputStream;%0A%09%09if (cacheTimeOut %3E 0) %7B%0A%09
 %09%09if (is
@@ -2039,24 +2039,25 @@
  != null) %7B%0A
+%09
 %09%09%09int readL
@@ -2066,16 +2066,17 @@
 gth;%0A%09%09%09
+%09
 byte%5B%5D b
@@ -2098,16 +2098,17 @@
 e%5B128%5D;%0A
+%09
 %09%09%09FileO
@@ -2174,16 +2174,17 @@
 le);%0A%09%09%09
+%09
 while ((
@@ -2236,24 +2236,25 @@
 )) != -1) %7B%0A
+%09
 %09%09%09%09fileOutp
@@ -2295,18 +2295,20 @@
 th);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09fileO
@@ -2334,24 +2334,14 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-InputStream 
+%09
 inpu
@@ -2382,16 +2382,52 @@
 eFile);%0A
+%09%09%7D else %7B%0A%09%09%09inputStream = is;%0A%09%09%7D%0A
 %09%09jsonRe
@@ -2478,31 +2478,8 @@
 m);%0A
-%09%09inputStream.close();%0A
 %09%7D%0A%7D

Fix the input stream not assigned while not using cache