2012-05-17 10 views
7

मैं इसके उत्तर के लिए चारों ओर देख रहा हूं, लेकिन वास्तव में कुछ भी नहीं मिला। इससे पहले, मैंने पूछा कि मैं बाद में पुनर्प्राप्ति के लिए एक बाइट सरणी के माध्यम से एक स्ट्रिंग में फ़ाइल कैसे बना सकता हूं, और फिर फिर से वापस कर सकता हूं।BufferedInputStream बाइट [] में एक सॉकेट पर एक सॉकेट पर भेजने के लिए

लोगों ने मुझे क्या बताया, यह था कि मुझे बुरा एन्कोडिंग समस्याओं से बचने के लिए बाइट सरणी को स्टोर करना था। तो अब मैंने उस पर काम करना शुरू कर दिया है, लेकिन अब मैंने एक दीवार मारा है।

मूल रूप से, मैंने एक बाइट सरणी में फ़ाइल को चालू करने के लिए पहले unbuffered स्ट्रीम का उपयोग किया था। यह सिद्धांत में अच्छा काम करता है, लेकिन इसमें बहुत सारी मेमोरी होती है जो अंततः ढेर आकार अपवाद डालेगी। मुझे इसके बजाय buffered स्ट्रीम का उपयोग करना चाहिए (या तो मुझे बताया गया है), और अब मेरी समस्या है, BufferedInputStream से एक बाइट [] पर जा रहा है। मैं कॉपी और इस दस्तावेज़

http://docs.guava-libraries.googlecode.com/git/javadoc/index.html?com/google/common/io/package-summary.html

मैं बफ़र धाराओं के लिए unbuffered धाराओं कहाँ का आदान-प्रदान में तरीकों पाया उपयोग करने के लिए कोशिश की है। एकमात्र मुद्दा यह है कि मैं एक बफर्ड आउटपुट स्ट्रीम को बाइट सरणी में सीधे नहीं बदल सकता, क्योंकि मैं एक अप्रयुक्त स्ट्रीम के साथ कर सकता हूं।

सहायता? :)

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

public final class BufferedByteStream { 

    private static final int BUF_SIZE = 1024000; 

    public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException { 
     byte[] buf = new byte[BUF_SIZE]; 
     long total = 0; 
     while(true) { 
      int r = from.read(buf); 
      if(r == -1) { 
       break; 
      } 
      to.write(buf, 0, r); 
      total += r; 
     } 
     return total; 
    } 

    public static byte[] toByteArray(BufferedInputStream in) throws IOException { 
     BufferedOutputStream out = new BufferedOutputStream(new ByteArrayOutputStream()); 
     copy(in, out); 
     return out. // <--- Problem is here 
    } 
} 

संपादित करें:

मैं अभी भी हो रही है ढेर अंतरिक्ष त्रुटियों।

main.java

import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import jserver.io.BufferedByteStream; 
/** 
* 
* @author Vipar 
*/ 
public class main { 
    public static void main(String[] args) { 
    File f = new File("<doesn't matter>"); 
     try { 
      byte[] buf; 
      try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { 
       buf = BufferedByteStream.toByteArray(bis); 
       bis.close(); 
      } 
      File f2 = new File("<doesn't matter>"); 
      try (FileOutputStream fos = new FileOutputStream(f2)) { 
       fos.write(buf); 
       fos.close(); 
      } 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

BufferedByteStream: तो अब मैं सभी कोड पोस्ट करेंगे।जावा

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

public final class BufferedByteStream { 

    private static final int BUF_SIZE = 1024000; 

    public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException { 
     byte[] buf = new byte[BUF_SIZE]; 
     long total = 0; 
     while(true) { 
      int r = from.read(buf); 
      if(r == -1) { 
       break; 
      } 
      to.write(buf, 0, r); 
      total += r; 
     } 
     return total; 
    } 

    public static byte[] toByteArray(BufferedInputStream in) throws IOException { 
     ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 
     BufferedOutputStream out = new BufferedOutputStream(bytesOut); 
     copy(in, out); 
     return bytesOut.toByteArray(); 
    } 
} 
+0

आधे समाधान के साथ क्या गलत है, यह पूछने के बजाय आप हमें बताएंगे कि आप किस समस्या को हल करने की कोशिश कर रहे हैं? – EJP

+0

मुझे मूल रूप से अब मुझे एहसास होना चाहिए था, लेकिन मैं इसे एक उचित प्रश्न में करूँगा जहां मैंने बस यह निर्धारित किया कि मैं क्या हासिल करना चाहता हूं। – OmniOwl

उत्तर

9

ByteArrayOutputStream पर एक नज़र डालें: Java 7 API java.io.ByteArrayOutputStream

bytesOut = new ByteArrayOutputStream(); 
byte[] bytes = bytesOut.toByteArray(); 

अद्यतन: तुम क्या कर तुम क्या तुम सिर्फ एक चर करने के लिए मध्यवर्ती ByteArrayOutputStream निर्दिष्ट करेंगे और उसे पकड़ प्राप्त कर सकते हैं कर रहे हैं पर जोर देते हैं सरणी इस तरह से:

ByteArrayOutputStream bytesOut = new ByteArrayOutputStream() 
BufferedOutputStream out = new BufferedOutputStream(bytesOut); 
copy(in, out); 
return bytesOut.toByteArray(); 

अद्यतन 2: असली सवाल प्रतीत होता है

FileChannel from = new FileInputStream(sourceFile).getChannel(); 
FileChanngel to = new FileOutputStream(destFile).getChannel(); 
to.transferFrom(from, 0, from.size()); 
//or from.transferTo(0, from.size(), to); 
from.close(); 
to.close(); 

3): ओएस और कोई पाश आदि द्वारा

byte[] buff = new byte[64*1024]; //or some size, can try out different sizes for performance 
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("fromFile")); 
    BufferedOutputStream out = new BufferedOutputStream(new FileoutputStream("toFile")); 
    int n = 0; 
    while ((n = in.read(buff)) >= 0) { 
     out.write(buff, 0, n); 
    } 
    in.close(); 
    out.close(); 

2) कुशलतापूर्वक: यह सब स्मृति में पहले पढ़ने के बिना एक फ़ाइल की प्रतिलिपि करने के लिए कैसे हो:

1) मैन्युअल आप जावा 7 है, तो आप अपवाद और धारा समापन सरल बना सकते हैं या सिर्फ नए एपीआई मैं के साथ फाइल कॉपी जावा 7 में:

java.nio.file.Files.copy(...); 

see java.nio.file.Files

+0

मेरी समझ के लिए, यह buffered नहीं है ... – OmniOwl

+0

आपको किसी और बफरिंग की आवश्यकता नहीं है ... यह स्मृति में एक सरणी बना रहा है। यह उससे अधिक buffered नहीं मिलता है। –

+0

हालांकि यदि आप मुझ पर विश्वास नहीं करते हैं (आपको वैसे भी खुद को मापना चाहिए), तो आप इसे किसी भी तरह से उपयोग कर सकते हैं लेकिन इसे अपने स्वयं के चर के बीच में असाइन कर सकते हैं। मैं उदाहरण अपडेट करूंगा। –

-1

DataFetcher इस के लिए एकदम सही होगा:

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/DataFetcher.java?revision=34&view=markup

आप बफर बाहर खाली करने के लिए उपयोग कर सकते हैं clearBuffer पढ़ता के बीच अगर आप स्मृति से बाहर चल रहे हैं

तुम भी आवश्यकता होगी टाइमआउट क्लास - यह एक ही पैकेज में एक ही पैकेज में है।

संबंधित मुद्दे