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 public GZIPChannel () { 039 super(); 040 } 041 /** 042 * Construct client ISOChannel 043 * @param host server TCP Address 044 * @param port server port number 045 * @param p an ISOPackager 046 * @see ISOPackager 047 */ 048 public GZIPChannel (String host, int port, ISOPackager p) { 049 super(host, port, p); 050 } 051 /** 052 * Construct server ISOChannel 053 * @param p an ISOPackager 054 * @exception IOException 055 * @see ISOPackager 056 */ 057 public GZIPChannel (ISOPackager p) throws IOException { 058 super(p); 059 } 060 /** 061 * constructs a server ISOChannel associated with a Server Socket 062 * @param p an ISOPackager 063 * @param serverSocket where to accept a connection 064 * @exception IOException 065 * @see ISOPackager 066 */ 067 public GZIPChannel (ISOPackager p, ServerSocket serverSocket) 068 throws IOException 069 { 070 super(p, serverSocket); 071 } 072 /** 073 * @param len the packed Message len 074 * @exception IOException 075 */ 076 protected void sendMessageLength(int len) throws IOException { 077 serverOut.write (len >> 8); 078 serverOut.write (len); 079 } 080 /** 081 * @return the Message len 082 * @exception IOException, ISOException 083 */ 084 protected int getMessageLength() throws IOException, ISOException { 085 int l = 0; 086 byte[] b = new byte[4]; 087 while (l == 0) { 088 serverIn.readFully(b,0,2); 089 l = ((int)b[0] &0xFF) << 8 | (int)b[1] &0xFF; 090 if (l == 0) { 091 serverOut.write(b); 092 serverOut.flush(); 093 } 094 } 095 return l; 096 } 097 protected void sendMessage (byte[] b, int offset, int len) 098 throws IOException 099 { 100 GZIPOutputStream gzip = new GZIPOutputStream(serverOut); 101 gzip.write(b, offset, len); 102 gzip.finish(); 103 gzip.flush(); 104 } 105 protected void getMessage (byte[] b, int offset, int len) throws IOException, ISOException { 106 int total = 0; 107 GZIPInputStream gzip = new GZIPInputStream(serverIn); 108 while (total < len) { 109 int nread = gzip.read (b, offset, len - total); 110 if (nread == -1) { 111 throw new ISOException("End of compressed stream reached before all data was read"); 112 } 113 total += nread; 114 offset += nread; 115 } 116 } 117} 118