getBytes doido

Seguinte:

	Blob b = rs.getBlob("ARQUIVO");
                        BufferedOutputStream os;
                        File file = new File(path + "\\" + rs.getString("NOME"));
								System.out.println(path + "\\" + rs.getString("NOME"));
                        os = new BufferedOutputStream(new FileOutputStream(file));
								System.out.println(b.length() + " é o comprimento");
                        os.write(b.getBytes(0, (int) b.length()), 0, (int) b.length());
                        os.flush();
                        os.close();
O que tem d errado neste código? ele está lançando a segunte exceção:
18944 é o comprimento
java.sql.SQLException: Position 'pos' can not be < 1
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
	at com.mysql.jdbc.Blob.getBytes(Blob.java:136)
	at Tab$1.mouseClicked(Tab.java:154)
	at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
	at java.awt.Component.processMouseEvent(Component.java:6266)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6028)
	at java.awt.Container.processEvent(Container.java:2041)
	at java.awt.Component.dispatchEventImpl(Component.java:4630)
	at java.awt.Container.dispatchEventImpl(Container.java:2099)
	at java.awt.Component.dispatchEvent(Component.java:4460)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
	at java.awt.Container.dispatchEventImpl(Container.java:2085)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4460)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Achei uma ajuda na pág do apache, dis que lança essa exceção qdo
"São lançadas exceções se pos < 1, se pos for maior que o comprimento, ou se length <= 0"

length tá normal, com podem ver acima da exceção. Não manjo essa parte de binario, se alguem puder ajudar, fico agradecido!
valeu

Isso que dá não ler a documentação.

[quote]Parameters:
pos - the ordinal position of the first byte in the BLOB value to be extracted; the first byte is at position 1
length - the number of consecutive bytes to be copied [/quote]

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Blob.html#getBytes(long,%20int)

Então, já tentei modificar… poder ser 1 ou pode ser qualquer numero. Omproblema é que ele corrompe o arquivo. Pior: temuma imagem gif que ele sempre carrega corretamente… to levando um coro, e ficando loco…

acho que deve ter algum ponto pra começar, até isso o banco pode ser que use, tipo, os primeiros bytes pra registrar otamanho… não sei não sei… dá mais uma luz ae…

mas valeu por enquanto

O código de inserção é o seguinte:

try{
                           File f = new File(valores[n]);
                           arq = valores[n];
                           FileInputStream fis = new FileInputStream(f);
                           byte[] tmp = new byte[1024];
                           byte[] data = null;
                           int sz, len = 0;
                        
                           while ((sz = fis.read(tmp)) != -1) {
                              if (data == null) {
                                 len = sz;
                                 data = tmp;
                              } 
                              else {
                                 byte[] narr;
                                 int nlen;
                              
                                 nlen = len + sz;
                                 narr = new byte[nlen];
                                 System.arraycopy(data, 0, narr, 0, len);
                                 System.arraycopy(tmp, 0, narr, len, sz);
                                 data = narr;
                                 len = nlen;
                              }
                           }
                           if (len != data.length) {
                              byte[] narr = new byte[len];
                           
                              System.arraycopy(data, 0, narr, 0, len);
                              data = narr;
                           }
                        
                        
                        
                           stmt.setObject(n+1,  data);
                          
                        
                        	
                        }
                            catch(Exception e){
                              e.printStackTrace();
                              stmt.setNull(n+1, Types.BLOB);
                              blob = false;
                           
                           }

Já tentei trocar aqueles valores do array, mas o arquivo continua voltando corrompido, se alguem souber dá uma ajuda ae.
valeu

Cara, q capiauzisse q eu fiz…
o codigo pra leitura tá certo, o errado é o de inserção.
o melhor é:

[code]
File f = new File(valores[n]);
arq = valores[n];
FileInputStream fis = new FileInputStream(f);

                    stmt.setBinaryStream(n+1, fis, (int) f.length());
                       
                      
    [/code]

Se não me engano, o primeiro parametro de getBytes deve ser 1, não zero.