package com.flytxt.tp.fileutils.loader;

import java.io.BufferedReader;
import java.io.IOException;

import com.flytxt.tp.fileutils.DiskChannel;
import com.flytxt.tp.lookup.Lookup;
import com.flytxt.tp.marker.MarkerFactory;

public class FileLoader implements Loader {
	private String fileName;
	private MarkerFactory mf;
	
    public FileLoader(String fileName, MarkerFactory mf) {
    		this.fileName = fileName;
    		this.mf = mf;
    }
    
    @Override
    public void load(Lookup lookup) {
    	
    	try(final DiskChannel channel = new DiskChannel()){
        	final BufferedReader bufferedReader =  	channel.open(fileName);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                final String key = line.substring(0, line.indexOf('|'));
                final String value = line.substring(key.length()+1, line.length());
                final byte[] valueByteArray = value.getBytes();

                if (!key.isEmpty())
                	lookup.load(key.getBytes(), mf.createMarker(valueByteArray, 0, valueByteArray.length));
            }
        } catch (final IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (Exception e1) {
			e1.printStackTrace();
		}
       
	}
}

@@ -315,22 +315,18 @@
 ory mf;%0A
+%0A
 %09
-%0A    
 public F
@@ -372,20 +372,16 @@
 y mf) %7B%0A
-    
 %09%09this.f
@@ -400,20 +400,16 @@
 leName;%0A
-    
 %09%09this.m
@@ -420,23 +420,13 @@
 mf;%0A
-    %7D%0A    %0A    
+%09%7D%0A%0A%09
 @Ove
@@ -431,20 +431,17 @@
 verride%0A
-    
+%09
 public v
@@ -470,22 +470,15 @@
 ) %7B%0A
-    %09%0A    
+%0A%09
 %09try
+ 
 (fin
@@ -520,26 +520,21 @@
 annel())
+ 
 %7B%0A
-        
+%09%09
 %09final B
@@ -564,18 +564,16 @@
 eader = 
- %09
 channel.
@@ -588,28 +588,19 @@
 eName);%0A
-            
+%09%09%09
 String l
@@ -604,28 +604,19 @@
 g line;%0A
-            
+%09%09%09
 while ((
@@ -660,32 +660,20 @@
 null) %7B%0A
-                
+%09%09%09%09
 final St
@@ -721,32 +721,20 @@
 ('%7C'));%0A
-                
+%09%09%09%09
 final St
@@ -777,9 +777,11 @@
 th()
-+
+ + 
 1, l
@@ -795,32 +795,20 @@
 gth());%0A
-                
+%09%09%09%09
 final by
@@ -848,32 +848,20 @@
 tes();%0A%0A
-                
+%09%09%09%09
 if (!key
@@ -876,24 +876,12 @@
 ())%0A
-                
+%09%09%09%09
 %09loo
@@ -969,30 +969,15 @@
 ));%0A
-            %7D%0A        
+%09%09%09%7D%0A%09%09
 %7D ca
@@ -987,18 +987,16 @@
  (final 
-IO
 Exceptio
@@ -1002,28 +1002,19 @@
 on e) %7B%0A
-            
+%09%09%09
 e.printS
@@ -1030,20 +1030,11 @@
 ();%0A
-            
+%09%09%09
 thro
@@ -1077,77 +1077,11 @@
 ));%0A
-        %7D catch (Exception e1) %7B%0A%09%09%09e1.printStackTrace();%0A%09%09%7D%0A       
+%09%09%7D
 %0A%09%7D%0A

refactor code