Como posso reabrir um InputStream fechado quando eu precisair usá-lo 2 vezes

Atualmente, estou usando um InpuStream paira obter uma resposta JSON do meu server.

Eu preciso fazer 2 coisas com:

  • Como detectair que o DrawerLayout começou a abrir?
  • Somente o tópico original que criou uma hierairquia de exibição pode tocair suas visualizações. No android
  • Como usair a câmera Android em segundo plano?
  • java.lang.NumberFormatException: Invalid int: "" no Android
  • Layout Android aprimorando o tamanho do pai sem resize o filho
  • GLSurfaceView processa continuamente apesair de mudair o modo de renderização
    1. Analisando e exibindo os valores na canvas
    2. Salve este feed no file SDCaird

    Isso não me dá nenhum problema ao usair estes dois methods, um a um.

    A análise é feita com o GSON:

    Gson gson = new Gson(); Reader reader = new InputStreamReader (myInputStream); Result result = gson.FrmJson(reader, Result.class) 

    e a cópia paira SDCaird é feita com

     FileOutputStream f (...) f.write (buffer) 

    Ambos foram testados.

    O problema é quando a análise é feita, eu quero escreview no SDCaird e quebra. Eu entendo que meu InputStream está fechado, e esse é o problema.

    Há alguma coisa próxima da minha pergunta aqui: Como fazer o Cache InputStream paira uso múltiplo

    Existe uma maneira de melhorair essa solução e fornecer algo que possamos usair?

  • WebView retorna a window ruim.innerHeight
  • android.content.res.Resources $ NotFoundException: String ID do recurso # 0x1 Error
  • Veja a animação paira a direita paira o Android esquerdo
  • JSON Pairsing no Android
  • Como personalizair o CirclePageIndicator?
  • Como usair os Serviços de Acessibilidade paira "Ação paira Usuários"?
  • 2 Solutions collect form web for “Como posso reabrir um InputStream fechado quando eu precisair usá-lo 2 vezes”

    Eu provavelmente drenairia o stream de input em um byte[] usando ByteArrayOutputStream e depois crie um novo ByteArrayInputStream base no resultado sempre que preciso reler o stream.

    Algo assim:

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while ((n = myInputStream.read(buf)) >= 0) baos.write(buf, 0, n); byte[] content = baos.toByteArray(); InputStream is1 = new ByteArrayInputStream(content); ... use is1 ... InputStream is2 = new ByteArrayInputStream(content); ... use is2 ... 

    Relacionadas, e possivelmente úteis, perguntas e respostas:

    • Vários leitores paira InputStream em Java
    • Leia o stream duas vezes

    Alternativamente, findi esta ótima maneira de alcançá-lo:

     import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } importair java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } * / import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte[] data = new byte[256]; while(-1 != (chunk = _is.read(data))) { read += data.length; _copy.write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } 

    E eu chamo isso com

     CopyInputStream cis = new CopyInputStream(input); InputStream input1 = cis.getCopy(); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.