001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso.channel;
020
021import org.jpos.iso.*;
022
023import java.io.IOException;
024import java.net.ServerSocket;
025import java.util.zip.GZIPInputStream;
026import java.util.zip.GZIPOutputStream;
027
028/**
029 * ISOChannel implementation.
030 *
031 * @author apr@jpos.org
032 * @version $Id$
033 * @see ISOMsg
034 * @see ISOException
035 * @see ISOChannel
036 */
037public class GZIPChannel extends BaseChannel {
038    /** Default constructor. */
039    public GZIPChannel () {
040        super();
041    }
042    /**
043     * Construct client ISOChannel
044     * @param host  server TCP Address
045     * @param port  server port number
046     * @param p     an ISOPackager
047     * @see ISOPackager
048     */
049    public GZIPChannel (String host, int port, ISOPackager p) {
050        super(host, port, p);
051    }
052    /**
053     * Construct server ISOChannel
054     * @param p     an ISOPackager
055     * @exception IOException on I/O error
056     * @see ISOPackager
057     */
058    public GZIPChannel (ISOPackager p) throws IOException {
059        super(p);
060    }
061    /**
062     * constructs a server ISOChannel associated with a Server Socket
063     * @param p     an ISOPackager
064     * @param serverSocket where to accept a connection
065     * @exception IOException on I/O error
066     * @see ISOPackager
067     */
068    public GZIPChannel (ISOPackager p, ServerSocket serverSocket) 
069        throws IOException
070    {
071        super(p, serverSocket);
072    }
073    /**
074     * Constructs a packager with the given length and description.
075     * @param len the packed Message len
076     * @exception IOException on I/O error
077     */
078    protected void sendMessageLength(int len) throws IOException {
079        serverOut.write (len >> 8);
080        serverOut.write (len);
081    }
082    /**
083     * @return the Message len
084     * @exception IOException on I/O error
085     * @exception ISOException on ISO processing error
086     */
087    protected int getMessageLength() throws IOException, ISOException {
088        int l = 0;
089        byte[] b = new byte[4];
090        while (l == 0) {
091            serverIn.readFully(b,0,2);
092            l = ((int)b[0] &0xFF) << 8 | (int)b[1] &0xFF;
093            if (l == 0) {
094                serverOut.write(b);
095                serverOut.flush();
096            }
097        }
098        return l;
099    }
100    protected void sendMessage (byte[] b, int offset, int len) 
101        throws IOException
102    {
103        GZIPOutputStream gzip = new GZIPOutputStream(serverOut);
104        gzip.write(b, offset, len);
105        gzip.finish();
106        gzip.flush();
107    }
108    protected void getMessage (byte[] b, int offset, int len) throws IOException, ISOException { 
109        int total = 0;
110        GZIPInputStream gzip = new GZIPInputStream(serverIn);
111        while (total < len) {
112                int nread = gzip.read (b, offset, len - total);
113                if (nread == -1) {
114                        throw new ISOException("End of compressed stream reached before all data was read"); 
115                }
116                total += nread;
117                offset += nread;
118        }
119    }
120}
121