Advertisement
etlon

Untitled

Feb 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package com.multistorage;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9.  
  10. import com.google.gson.Gson;
  11. import com.google.gson.JsonIOException;
  12. import com.google.gson.JsonSyntaxException;
  13. import com.storage.Article;
  14.  
  15. public class StorageSystem
  16. {
  17.    
  18.     private static final String STORAGE_FILE_TYPE = ".json";
  19.     private static final String STORAGE_FILE_NAME_PREFIX = "storage";
  20.     private static final int STORAGE_FILE_SPLIT_SIZE = 100000;
  21.     private final String PATH_TO_LINK_FILE = "src/com/multistorage/visitedlinks.txt";
  22.     private final String PATH_TO_STORAGE_FOLDER;
  23.     private int amountFilesInFolder;
  24.     private int currentFileSize;
  25.     private File latestFile;
  26.     private Gson gson;
  27.     private FileReader fr;
  28.     private FileWriter jsonWriter;
  29.     private FileWriter linkWriter;
  30.  
  31.    
  32.     public StorageSystem(String PATH_TO_STORAGE_FOLDER)
  33.     {
  34.         this.PATH_TO_STORAGE_FOLDER = PATH_TO_STORAGE_FOLDER;
  35.         this.amountFilesInFolder = getAmountFilesInFolder();
  36.         this.gson = new Gson();
  37.        
  38.        
  39.     }
  40.    
  41.     public void store(Article article, String linkToCheck) throws IOException
  42.     {
  43.        
  44.         this.amountFilesInFolder = getAmountFilesInFolder();
  45.         if(amountFilesInFolder == 0)
  46.         {
  47.             latestFile = new File(PATH_TO_STORAGE_FOLDER + "\\" + STORAGE_FILE_NAME_PREFIX + amountFilesInFolder + STORAGE_FILE_TYPE);
  48.             System.out.println(latestFile.getAbsolutePath());
  49.             //System.out.println(latestFile.getPath());
  50.             try
  51.             {
  52.                 latestFile.createNewFile();
  53.             } catch (IOException e)
  54.             {
  55.                 e.printStackTrace();
  56.             }
  57.            
  58.         }
  59.         latestFile = getLatestFile();
  60.         jsonWriter = new FileWriter(latestFile);
  61.         linkWriter = new FileWriter(PATH_TO_LINK_FILE, true);
  62.        
  63.        
  64.        
  65.        
  66.        
  67.         Article[] contentOfFileArray = null;
  68.        
  69.         try
  70.         {
  71.             contentOfFileArray = gson.fromJson(new FileReader(latestFile), Article[].class);
  72.         } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e1)
  73.         {
  74.             e1.printStackTrace();
  75.         }
  76.        
  77.        
  78.  
  79.         //System.out.println(contentOfFileArray.length);
  80.         if(contentOfFileArray == null)
  81.         {
  82.             contentOfFileArray = new Article[0];
  83.         }
  84.         contentOfFileArray = getNewArticleArray(contentOfFileArray, article);
  85.         if(latestFile.length() < STORAGE_FILE_SPLIT_SIZE)
  86.         {
  87.  
  88.            
  89.             if(isDuplicate(new File(PATH_TO_LINK_FILE), linkToCheck) == false)
  90.             {
  91.                
  92.                 try
  93.                 {
  94.                     System.out.println(linkToCheck);
  95.                     System.out.println(latestFile.getPath());
  96.                     jsonWriter.write(gson.toJson(contentOfFileArray));
  97.                     jsonWriter.flush();
  98.                     int hashcode = linkToCheck.hashCode();
  99.                     linkWriter.write(hashcode + "\n");
  100.                     linkWriter.flush();
  101.                    
  102.                 } catch (Exception e)
  103.                 {
  104.                    
  105.                 }
  106.             }
  107.         } else
  108.         {
  109.            
  110.             System.out.println("Datei ist zu groß. Neue Datei wird erstellt.");
  111.         }
  112.  
  113.  
  114.        
  115.        
  116.        
  117.        
  118.        
  119.        
  120.     }
  121.    
  122.     public int getAmountFilesInFolder()
  123.     {
  124.         return new File(PATH_TO_STORAGE_FOLDER).list().length;
  125.     }
  126.    
  127.     public File getLatestFile()
  128.     {
  129.        
  130.         File[] fileArray = new File(PATH_TO_STORAGE_FOLDER).listFiles();
  131.        
  132.         return fileArray[fileArray.length - 1];
  133.     }
  134.    
  135.     public Article[] getNewArticleArray(Article[] oldArray, Article article)
  136.     {
  137.         Article[] oldArrayCopy = new Article[oldArray.length + 1];
  138.         for(int i = 0; i < oldArray.length - 1; i++)
  139.         {
  140.             oldArrayCopy[i] = oldArray[i];
  141.         }
  142.         oldArrayCopy[oldArrayCopy.length - 1] = article;
  143.        
  144.         return oldArrayCopy;
  145.     }
  146.    
  147.     public boolean isDuplicate(File linkFile, String link) throws IOException
  148.     {
  149.         Scanner scan = new Scanner(linkFile);
  150.        
  151.         int hashWert = link.hashCode();
  152.        
  153.        
  154.         while(scan.hasNextLine())
  155.         {
  156.            
  157.             String line = scan.nextLine();
  158.             int lineWert = Integer.parseInt(line);
  159.            
  160.             if(lineWert == hashWert)
  161.             {
  162.                 return true;
  163.             }
  164.         }
  165.         return false;
  166.     }
  167.    
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement