Last active
February 3, 2017 09:02
-
-
Save gordinmitya/727c92df0d3a9b343213bc84843003f5 to your computer and use it in GitHub Desktop.
Пример безопасной работы с потоками
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GameManager.java | |
public class GameManager { | |
private GameManager() {} | |
public static create(){ | |
GameManager gm = new GameManager(); | |
gm. // инициализируем созданием нового | |
return gm; | |
} | |
public static createFromStream(InputStream stream){ | |
GameManager gm = new GameManager(); | |
gm. // инициализируем значениями считанными из потока | |
return gm; | |
} | |
} | |
// MainActivity.java | |
private final String MAZE_KEY = "Maze"; | |
// .... | |
if (savedInstanceState != null) { | |
InputStream mazeStream; | |
try { | |
try { | |
mazeStream = new ByteArrayInputStream(savedInstanceState.getByteArray(MAZE_KEY)); | |
gameManager = GameManager.createFromStream(mazeStream); | |
} finally { | |
if (mazeStream != null) { | |
mazeStream.close(); | |
} | |
} | |
} catch (IOException e) { | |
Log.e(MainActivity.class.getName(),"Ошибка чтения состояния из потока : "+e.getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment