2012-04-03 1 views
0
public class GenericWorldLoader implements WorldLoader { 
    @Override 
    public LoginResult checkLogin(PlayerDetails pd) { 
     Player player = null; 
     int code = 2; 
     File f = new File("data/savedGames/" + NameUtils.formatNameForProtocol(pd.getName()) + ".dat.gz"); 
     if(f.exists()) { 
      try { 
       InputStream is = new GZIPInputStream(new FileInputStream(f)); 
       String name = Streams.readRS2String(is); 
       String pass = Streams.readRS2String(is); 
       if(!name.equals(NameUtils.formatName(pd.getName()))) { 
        code = 3; 
       } 
       if(!pass.equals(pd.getPassword())) { 
        code = 3; 
       } 
      } catch(IOException ex) { 
       code = 11; 
      } 
     } 
     if(code == 2) { 
      player = new Player(pd); 
     } 
     return new LoginResult(code, player); 
    } 

    @Override 
    public boolean savePlayer(Player player) { 
     try { 
      OutputStream os = new GZIPOutputStream(new FileOutputStream("data/savedGames/" + NameUtils.formatNameForProtocol(player.getName()) + ".dat.gz")); 
      IoBuffer buf = IoBuffer.allocate(1024); 
      buf.setAutoExpand(true); 
      player.serialize(buf); 
      buf.flip(); 
      byte[] data = new byte[buf.limit()]; 
      buf.get(data); 
      os.write(data); 
      os.flush(); 
      os.close(); 
      return true; 
     } catch(IOException ex) { 
      return false; 
     } 
    } 

    @Override 
    public boolean loadPlayer(Player player) { 
     try { 
      File f = new File("data/savedGames/" + NameUtils.formatNameForProtocol(player.getName()) + ".dat.gz"); 
      InputStream is = new GZIPInputStream(new FileInputStream(f)); 
      IoBuffer buf = IoBuffer.allocate(1024); 
      buf.setAutoExpand(true); 
      while(true) { 
       byte[] temp = new byte[1024]; 
       int read = is.read(temp, 0, temp.length); 
       if(read == -1) { 
        break; 
       } else { 
        buf.put(temp, 0, read); 
       } 
      } 
      buf.flip(); 
      player.deserialize(buf); 
      return true; 
     } catch(IOException ex) { 
      return false; 
     } 
    } 

} 

Да, так ... Моя проблема в том, что это, похоже, спасает «что-то» в действительно сложном и трудно читаемом виде (двоичном), и я бы предпочел использовать его как .txt, в легко читаемый формат. как конвертировать?Проблема с фрагментами java

EDIT: Я не использую Apache Мину, так что я должен заменить

IoBuffer buf = IoBuffer.allocate(1024); 
buf.setAutoExpand(true);" 

с?

ответ

0

checkLogin() Очевидно, проверяет, имеет ли указанный логин соответствующие данные и правилен ли пароль.

savePlayer() метод сохраняет плеер.

loadPlayer() загружает его снова.

Используемый формат данных: gzip (wiki) и записывается как поток данных serialized. Если вы хотите сделать его более читаемым, вы можете захотеть перегрузить (или просто использовать его, если это хорошо) toString() метод класса Player и написать player.toString() в новый текстовый файл, используя, например, BufferedWriter, обернутый вокруг a File Writer:

String playerName = NameUtils.formatNameForProtocol(player.getName()); 
BufferedWriter writer = new BufferedWriter(new FileWriter(playerName + ".txt")); 
writer.write(player.toString()); 
writer.close(); 

 Смежные вопросы

  • Нет связанных вопросов^_^